Display joined table records in one parent record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    Display joined table records in one parent record

    Hey guys, I think this is very easy to do and I'm just having a brain fart.

    I have a movie database that links to an actors database. 1 move = many actors of course but I want to display the actor information along with the movie like so

    series_id, name, actors
    123 | John Foo, Jay Bar, James Baz

    the two tables are linked by actor_id

    Here's what I got:

    Code:
    mysql> SELECT si.series_id, (SELECT a.full_name FROM actor AS a WHERE a.actor_id = sa.actor_id) AS actors
        -> FROM seriesimage AS si LEFT JOIN seriesactor AS sa ON sa.series_id = si.series_id
        -> WHERE si.series_id = 121428;
    +-----------+--------------------------+
    | series_id | actors                   |
    +-----------+--------------------------+
    |    121428 | Michael Clarke Duncan    |
    |    121428 | NULL                     |
    |    121428 | The Rock Douglas Johnson |
    +-----------+--------------------------+
    In my mind this is pretty popular and simple to do but I'm not putting the right keywords into Google.

    Thanks,



    Dan
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Try the following
    [CODE=mysql]SELECT si.series_id,
    GROUP_CONCAT(SE LECT a.full_name FROM actor AS a WHERE a.actor_id = sa.actor_id) AS actors
    FROM seriesimage AS si LEFT JOIN seriesactor AS sa ON sa.series_id = si.series_id
    WHERE si.series_id = 121428
    GROUP BY si.series_id;[/CODE]

    I suggest you not to mix subquery with JOIN. Use INNER JOIN to get the names of the actors.

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Sweet! I'll try that next time. I don't remember GROUP_CONCAT. I better look that up.

      You guys never disappoint the byte php forum n00bs!



      Dan

      Comment

      Working...