SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Little Rock
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query help: subquery or join?

    I have a database for a raffle that has 2 tables: one has entrant information and the other has entry numbers along with the id of the person who bought each number. So...
    TABLE 1 - entrants
    Code:
    id | name | address |phone
    ----------------------------
    1  | John  |123 St   |5551212
    ----------------------------
    2  | Mary  |456 Ave |5559999
    TABLE 2 - entries
    Code:
    entrant_id | entry_num
    ------------------------
        1         | 10001 
    ------------------------
        1         | 10002 
    ------------------------
        2         | 10003 
    ------------------------
        2         | 10004 
    ------------------------
        2         | 10005
    The entry numbers are randomly generated and therefore not sequential. This is just for the purpose of illustration.

    I am trying to craft a query that will give me something like
    Code:
    id | name | address |  phone  |  entries
    -------------------------------------------------
    1  | John  |123 St   |5551212 |10001,10002
    -------------------------------------------------
    2  | Mary  |456 Ave |5559999 |10003,10004,10005
    Since each entrant can have multiple entry numbers, if I use a join I'll end up a row for each entry number instead of a row for each entrant. It seems that a subquery may work but I can't get my head around how to do that. Maybe there's another way?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    the JOIN is correct
    Code:
    SELECT x.id
         , x.name
         , x.address
         , x.phone
         , GROUP_CONCAT(y.entry_num) AS entries
      FROM entrants AS x
    INNER
      JOIN entries AS y
        ON y.entrant_id = x.id
    GROUP
        BY x.id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Little Rock
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You make it look so easy, Rudy
    Thanks!!


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
  •