dongshai2022 2013-08-07 12:35 采纳率: 100%
浏览 18

如何在多行中使用相同ID在MYSQL中加入2个表

I'm having a hard time figuring this out. Hope someone can help me figure this out.


For example. I have a video site and in this video site I can submit video posts with the name of people who play in this video. But I don't want to add just 1 name it can be more. So i'd have a video.php page and in this page I can submit the video title of the video and the names of people who play in it.

table: post

    -------------------------------------------
  -- video_id ---- video_name ----  video_cast -- 
    -------------------------------------------
       1              Vid-1           1
       2              Vid-2           2
       3              Vid-2           2   

table: cast

    -----------------------------------------
  -- cast_id ----  cast_name ----  cast_video -- 
    -----------------------------------------
       1              John            1
       2              Erik            2
       3              Ela             2

Now if i would be on my homepage and I see this new post. If I click on it. It will send the video_id information in the header so I can fetch it from the video.php.

page: video.php

<?php   

    $id = $_GET['id'];
    $result = mysql_query
    ("
        SELECT * 
        FROM post 
        LEFT JOIN cast 
        ON post.video_cast = cast.cast_video
        WHERE video_id='$id' 
    ");

    while($row = mysql_fetch_array($result)){
        echo ' 
                   <h1>'.$row['video_name'].'</h1> 
                   starring: '.$row['cast_name'].
                 ';                   
    }
?>

Now how would I be able to show the video_name and all the cast_name that are associated with the video. The JOIN code I use can only show 1 name out of the table. Anybody know how I could fetch the other remaining name(s)?

  • 写回答

1条回答 默认 最新

  • dongzhen6554 2013-08-07 12:37
    关注

    You can list the names in one row with group_concat()

     SELECT video_name, group_concat(cast_name) cast
     FROM post 
     LEFT JOIN cast ON post.video_cast = cast.cast_video
     WHERE video_id='$id' 
     group by video_name
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程