Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: SQL help

  1. #1
    chrisgibbons is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    10

    Default SQL help

    Hi Gang,

    I have to create an SQL that displays opportunites and their associated account details (phone number, address etc).

    Sounds simple, but I am really not great with SQL

    Any help would be really helpful

    Thanks

    Chris

  2. #2
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: SQL help

    This should give you the fields from the Opportunity and Accounts table in a single line:

    Code:
    SELECT a.*, o.* FROM opportunities o
    JOIN accounts_opportunities ao
    ON o.id = ao.opportunity_id
    JOIN accounts a
    ON a.id = ao.account_id
    Regards,

    Angel Magaņa
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  3. #3
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: SQL help

    Alter as needed

    Code:
    select opportunities.*, accounts.*
    from opportunities
    inner join accounts_opportunities on opportunities.id = accounts_opportunities.opportunity_id and accounts_opportunities.deleted =0
    inner join accounts on accounts_opportunities.account_id = accounts.id and accounts.deleted = 0
    where opportunities.deleted = 0

  4. #4
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: SQL help

    Hahah....we meet once again.
    Regards,

    Angel Magaņa
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  5. #5
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: SQL help

    It looks like you may have won the battle...

  6. #6
    chrisgibbons is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    10

    Default Re: SQL help

    Thanks you so much for the speedy help!

  7. #7
    chrisgibbons is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    10

    Default Re: SQL help

    What if I wanted to included fields form contacts ... eg. contact name email address etc?



    QUOTE=eggsurplus;188416]Alter as needed

    Code:
    select opportunities.*, accounts.*
    from opportunities
    inner join accounts_opportunities on opportunities.id = accounts_opportunities.opportunity_id and accounts_opportunities.deleted =0
    inner join accounts on accounts_opportunities.account_id = accounts.id and accounts.deleted = 0
    where opportunities.deleted = 0
    [/QUOTE]

  8. #8
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: SQL help

    Using the same principles join on accounts_contacts then to the contacts table. However, you'll get a row per contact that is associated to the account. I'll leave the actual sql to you on this one since you have an example to go off of already

  9. #9
    chrisgibbons is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    10

    Smile Re: SQL help

    Thanks again for the nudge.... this is what i have got

    select opportunities.name

    from opportunities

    inner join accounts_opportunities on opportunities.id = accounts_opportunities.opportunity_id

    inner join accounts on accounts_opportunities.account_id = accounts.id

    inner join accounts_contacts on opportunities.id = accounts_contacts.contact_id

    inner join contacts on accounts_contacts.contact_id = contacts.id

    No errors... but also not results?

    do i need to add something???

    I know I am rubbish at this!

  10. #10
    mikesolomon is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    UK
    Posts
    1,467

    Default Re: SQL help

    Try something like

    select o.name, a.name, c.last_name
    from opportunities o
    inner join accounts_opportunities ao on o.id = ao.opportunity_id and ao.`deleted` = 0
    inner join accounts a on ao.account_id = a.id and a.`deleted` = 0
    LEFT join `opportunities_contacts` oc on o.id = oc.`opportunity_id` and oc.`deleted` = 0
    left join contacts c on oc.contact_id = c.id and c.`deleted` = 0
    WHERE o.`deleted` = 0
    Mike Solomon
    Development Manager
    Ivy Ltd
    www.ivy.ltd.uk]www.ivy.ltd.uk

    php version 5.2.6
    MySql 5.1.59

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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
  •