Results 1 to 3 of 3

Thread: Help with complex SQL query

  1. #1
    aheuermann is offline Sugar Community Member
    Join Date
    May 2006
    Posts
    61

    Question Help with complex SQL query

    I am looking to create a query that ouputs the all the contacts exactly how they are in th list view.
    For every contact I want their last_name, first_name, title, email1, phone_work, and the name of the account(if they are associated with one).

    The accounts_contacts table is used to link any contacts to their corresponding account. I can get it to output all contacts who are assigned to an account, but I cant get it to output a list of all contacts that, if associated with an account, it is listed otherwise it is listed as Null.

    Thanks in advance for any help.

  2. #2
    stevec is offline Sugar Community Member
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,100

    Default Re: Help with complex SQL query

    This should do it - 'outer joins' are your friend...

    SELECT
    contacts.last_name,
    contacts.first_name,
    contacts.title,
    contacts.email1,
    contacts.phone_work,
    accounts.name
    FROM
    contacts
    Left Outer Join accounts_contacts ON contacts.id = accounts_contacts.contact_id
    Left Outer Join accounts ON accounts_contacts.account_id = accounts.id

  3. #3
    aheuermann is offline Sugar Community Member
    Join Date
    May 2006
    Posts
    61

    Default Re: Help with complex SQL query

    Thank you very much!!!

Thread Information

Users Browsing this Thread

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

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
  •