Results 1 to 2 of 2

Thread: Help with MySQL Query

  1. #1
    anicolais is offline Senior Member
    Join Date
    Nov 2009
    Posts
    99

    Default Help with MySQL Query

    Hi,
    I am trying to run the following query. In this query it shows all contacts with an email address but does not show the ones without an email address. This is because of the second to last line "AND contacts.id = email_addr_bean_rel.bean_id". What I need is to run all the lines except this one. Then also run all the lines with this one and put the 2 pieces of data together so I have the contacts with and without email addresses. Is there a way to do this with some sort of case/if statement or what would be the best way to go about this? I am doing this right from a command line.

    Thanks!

    accounts_cstm.result_status_date_c,accounts_cstm.L istsource_c, accounts_cstm.product_interest_c,accounts.name,con tacts.first_name,contacts.last_name, contacts.title,contacts.phone_work, email_addresses.email_address, contacts.primary_address_street,contacts.primary_a ddress_city, contacts.primary_address_state, contacts.primary_address_postalcode,primary_addres s_country,notes.description
    FROM notes, accounts, accounts_cstm, contacts, accounts_contacts,email_addr_bean_rel, email_addresses
    WHERE accounts.id = accounts_cstm.id_c
    AND accounts_contacts.contact_id = contacts.id
    AND accounts_contacts.account_id = accounts.id
    AND notes.contact_id = contacts.id
    AND accounts_cstm.result_status_c LIKE 'Qualified Lead%'
    AND contacts.id = email_addr_bean_rel.bean_id
    GROUP BY contacts.last_name

  2. #2
    datasponge is offline Sugar Community Member
    Join Date
    Mar 2008
    Location
    San Jose, CA, USA
    Posts
    553

    Default Re: Help with MySQL Query

    Quote Originally Posted by anicolais View Post
    Hi,
    I am trying to run the following query. In this query it shows all contacts with an email address but does not show the ones without an email address. This is because of the second to last line "AND contacts.id = email_addr_bean_rel.bean_id". What I need is to run all the lines except this one. Then also run all the lines with this one and put the 2 pieces of data together so I have the contacts with and without email addresses. Is there a way to do this with some sort of case/if statement or what would be the best way to go about this? I am doing this right from a command line.

    Thanks!

    accounts_cstm.result_status_date_c,accounts_cstm.L istsource_c, accounts_cstm.product_interest_c,accounts.name,con tacts.first_name,contacts.last_name, contacts.title,contacts.phone_work, email_addresses.email_address, contacts.primary_address_street,contacts.primary_a ddress_city, contacts.primary_address_state, contacts.primary_address_postalcode,primary_addres s_country,notes.description
    FROM notes, accounts, accounts_cstm, contacts, accounts_contacts,email_addr_bean_rel, email_addresses
    WHERE accounts.id = accounts_cstm.id_c
    AND accounts_contacts.contact_id = contacts.id
    AND accounts_contacts.account_id = accounts.id
    AND notes.contact_id = contacts.id
    AND accounts_cstm.result_status_c LIKE 'Qualified Lead%'
    AND contacts.id = email_addr_bean_rel.bean_id
    GROUP BY contacts.last_name
    Try changing:

    AND contacts.id = email_addr_bean_rel.bean_id

    to

    AND (contacts.id = email_addr_bean_rel.bean_id OR email_addr_bean_rel.bean_id IS NULL)

    I'm not sure if you need to explicitly specify a LEFT JOIN to email_addr_bean_rel table. I'm more familiar with the explicit join syntax like:

    SELECT contact.last_name, email_addresses.email_address FROM (contacts LEFT JOIN email_addr_bean_rel ON contacts.id = email_addr_bean_rel.contact_id) LEFT JOIN email_addresses ON email_addr_bean_rel.email_address_id = email_addresses.id WHERE (contacts.id = email_addr_bean_rel.bean_id OR email_addr_bean_rel.bean_id IS NULL);

    So you could also convert the query to something similar using explicit LEFT JOINs.

    Phil

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 2011-08-18, 01:48 PM
  2. Replies: 3
    Last Post: 2008-09-30, 08:43 PM
  3. Replies: 2
    Last Post: 2006-10-20, 12:00 AM
  4. Query Failed: ::MySQL error 1065: Query was empty
    By suruchi in forum Developer Help
    Replies: 1
    Last Post: 2006-01-21, 03:16 PM
  5. Query Failed: ::MySQL error 1065: Query was empty
    By suruchi in forum General Discussion
    Replies: 0
    Last Post: 2006-01-21, 12:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •