SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Is it possible to combine these two queries?

    Given the application I'm working on, it will simplify things greatly if these two queries can be combined. Is this possible?

    query 1:
    Select organization_name, email from partners

    query 2:
    SELECT count(*) as 'referrals' ,partner_id
    FROM partner_application_stories
    GROUP BY partner_id

    desired output:

    organization_name | email | referrals

    Thanks E

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by eruna View Post
    Given the application I'm working on, it will simplify things greatly if these two queries can be combined. Is this possible?

    query 1:
    Select organization_name, email from partners

    query 2:
    SELECT count(*) as 'referrals' ,partner_id
    FROM partner_application_stories
    GROUP BY partner_id

    desired output:

    organization_name | email | referrals

    Thanks E
    I am on my phone now, but if your partners table has a partner_id then you can do something like:
    Code:
    SELECT
      p.organization_name 
      , p.email
      , (SELECT
        count(*) 
         FROM
           partner_application_stories) AS 'referrals'
      , pas.partner_id
    FROM
      partners AS p
    INNER JOIN // Returns only matching rows if you want all rows in the p.partner table use LEFT OUTER JOIN or all in right the RIGHT OUTER JOIN
      partner_application_stories AS 'pas'
      ON p.partner_id = pas.partner_id
    GROUP BY pas.partner_id
    I won't be at a computer until tomorrow, but give this a try and see if it works for you.

    Regards,
    Steve
    ictus==""


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
  •