SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Dolbex's Avatar
    Join Date
    Jun 2003
    Location
    Confused USA
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Query to Include Count from Associated Table

    I am having a tough time coming up with the sql for the statement but the long and short of it is this:

    Two Tables:

    Table 1 is a group - Table 2 is a list of items that belong to a group from table 1.

    I want to query all (*) information from table 1 AND get the count of the number of associated items from that group from table 2 in that same query. Possible?

    Thanks!

    Dolbex

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    697
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select <columnsOfInterest>, 
           (select count(*)
              from groupItems as gi
             where gi.groupId = g.groupId) as items
       from groups as g

  3. #3
    SitePoint Enthusiast Dolbex's Avatar
    Join Date
    Jun 2003
    Location
    Confused USA
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mucho Gracias!


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
  •