Results 1 to 2 of 2

Thread: Why does this query not give me contacts without emails?

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

    Default Why does this query not give me contacts without emails?

    The following query shows all contacts with email addresses but does not show any contacts without email addresses. Why could this be? Thanks! SQL Server/Sugar 5.5CE

    SELECT (sugarcrm_db.dbo.contacts.last_name +', '+ sugarcrm_db.dbo.contacts.first_name) AS "Contact Name", (CASE WHEN sugarcrm_db.dbo.accounts.name IS NULL THEN '' ELSE sugarcrm_db.dbo.accounts.name END) AS "Company", (CASE WHEN sugarcrm_db.dbo.contacts.phone_work IS NULL THEN '' ELSE sugarcrm_db.dbo.contacts.phone_work END) AS "Work Phone", (CASE WHEN sugarcrm_db.dbo.contacts.phone_mobile IS NULL THEN '' ELSE sugarcrm_db.dbo.contacts.phone_mobile END) AS "Mobile Phone", email_addresses.email_address AS "Email"
    FROM sugarcrm_db.dbo.contacts LEFT JOIN sugarcrm_db.dbo.accounts_contacts ON sugarcrm_db.dbo.contacts.id = sugarcrm_db.dbo.accounts_contacts.contact_id
    LEFT JOIN sugarcrm_db.dbo.accounts ON sugarcrm_db.dbo.accounts.id = sugarcrm_db.dbo.accounts_contacts.account_id
    LEFT JOIN sugarcrm_db.dbo.email_addr_bean_rel ON sugarcrm_db.dbo.contacts.id = sugarcrm_db.dbo.email_addr_bean_rel.bean_id
    LEFT JOIN sugarcrm_db.dbo.email_addresses ON sugarcrm_db.dbo.email_addr_bean_rel.email_address_ id = sugarcrm_db.dbo.email_addresses.id
    WHERE sugarcrm_db.dbo.contacts.id = sugarcrm_db.dbo.email_addr_bean_rel.bean_id OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id = ''
    AND sugarcrm_db.dbo.contacts.assigned_user_id = '367b274c-0723-3b01-2b96-4b2a7b024731'
    AND sugarcrm_db.dbo.contacts.deleted = 0
    AND sugarcrm_db.dbo.email_addresses.deleted = 0
    AND sugarcrm_db.dbo.accounts_contacts.deleted = 0
    AND sugarcrm_db.dbo.email_addr_bean_rel.deleted = 0
    AND sugarcrm_db.dbo.accounts.deleted = 0
    ORDER BY sugarcrm_db.dbo.contacts.last_name

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

    Default Re: Why does this query not give me contacts without emails?

    Anicois,

    From a quick look I see two problems:
    1. WHERE Precedence for the OR vs AND
    2. Checking '' and 0 vs NULL

    Your WHERE clause is:
    Quote Originally Posted by anicolais View Post
    WHERE sugarcrm_db.dbo.contacts.id = sugarcrm_db.dbo.email_addr_bean_rel.bean_id OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id = ''
    AND sugarcrm_db.dbo.contacts.assigned_user_id = '367b274c-0723-3b01-2b96-4b2a7b024731'
    AND sugarcrm_db.dbo.contacts.deleted = 0
    AND sugarcrm_db.dbo.email_addresses.deleted = 0
    AND sugarcrm_db.dbo.accounts_contacts.deleted = 0
    AND sugarcrm_db.dbo.email_addr_bean_rel.deleted = 0
    AND sugarcrm_db.dbo.accounts.deleted = 0
    When there is an unmatched table in a LEFT JOIN, all fields in that table are NULL. So your checks against '' and 0 will not match any records and so will exclude all records as in the lines:
    OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id = ''
    AND sugarcrm_db.dbo.email_addresses.deleted = 0
    and
    AND sugarcrm_db.dbo.email_addr_bean_rel.deleted = 0
    You still get the matching records because your precedence in the first line is OR'ed before any of the AND's defeating the purpose of checking any of the deleted values.

    You need to use parenthesis and consider what you want to include to make it work they way you want like for example:
    Quote Originally Posted by anicolais View Post
    WHERE (sugarcrm_db.dbo.contacts.id = sugarcrm_db.dbo.email_addr_bean_rel.bean_id OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id IS NULL)
    AND sugarcrm_db.dbo.contacts.assigned_user_id = '367b274c-0723-3b01-2b96-4b2a7b024731'
    AND sugarcrm_db.dbo.contacts.deleted = 0
    AND (sugarcrm_db.dbo.email_addresses.deleted = 0 OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id IS NULL)
    AND sugarcrm_db.dbo.accounts_contacts.deleted = 0
    AND (sugarcrm_db.dbo.email_addr_bean_rel.deleted = 0 OR sugarcrm_db.dbo.email_addr_bean_rel.bean_id IS NULL)
    AND sugarcrm_db.dbo.accounts.deleted = 0
    You might also want to add a similar OR statement for accounts if unmatched accounts are ok.

    Phil

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Query Failed:INSERT into emails set
    By ftrevinoalbert in forum Help
    Replies: 1
    Last Post: 2008-10-07, 08:47 PM
  2. Replies: 2
    Last Post: 2008-08-07, 11:44 AM
  3. Emails table query slowness
    By rgodoy in forum Help
    Replies: 1
    Last Post: 2007-04-03, 08:23 PM
  4. How can I get vars from Contacts and give them to a jsp?
    By hectorimet in forum Developer Help
    Replies: 4
    Last Post: 2006-12-11, 08:02 AM
  5. Replies: 1
    Last Post: 2006-10-26, 06:30 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
  •