SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SELECT LEFT JOIN (and return array of records from right table)

    SELECT field, (array of values from table 2) FROM $table1 t1 LEFT JOIN $table2 ON t1.id=t2.id

    ... is something like this possible? (where I return records from t1 in a LEFT JOIN, but also get an array of any of the fields/records from the table 2)

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    a database query does not produce arrays

    a database query produces a result set, which is exactly like a flat file -- it has rows and columns

    other than that, yes, what you are asking is possible
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Can you help then?

  4. #4
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like...

    SELECT field (SELECT field1, field2 FROM $table2) FROM $table1 LEFT JOIN $table2 ....

    ???

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    you're new to joins? see this article: The FROM Clause
    Code:
    SELECT t1.field 
         , t2.field1
         , t2.field2 
      FROM $table1 AS t1
    LEFT OUTER
      JOIN $table2 AS t2
        ON t2.foo = t1.bar
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I'm pretty certain your query won't return a load of values from table 2.

    Table 1:
    name / id
    Dave / 1
    Steve / 2

    Table 2:
    id / amount
    1 / 10
    1 / 15

    I need to be able to return

    Dave (10, 15)

    I definitely don't need a right join (which I know you've not suggested). I need a left join, but to still get all the values from the right table (as shown just above).

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by johnsmith153 View Post
    Dave (10, 15)
    so, a comma-delimited list?

    mysql can do that
    Code:
    SELECT t1.name
         , GROUP_CONCAT(t2.amount) AS amounts
      FROM table1 AS t1
    LEFT OUTER
      JOIN table2 AS t2
        ON t2.id = t1.id
    GROUP
        BY t1.name
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks a lot!!


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
  •