如何在多行中以相同的ID在MYSQL中联接2个表 [英] How to JOIN 2 tables in MYSQL with same ID on multiple rows

查看:119
本文介绍了如何在多行中以相同的ID在MYSQL中联接2个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解决这个问题.希望有人可以帮助我解决这个问题.

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

例如.我有一个视频网站,在这个视频网站中,我可以提交视频帖子,并以该视频中的播放者的名字命名.但我不想仅添加1个名称,也可以添加更多名称.因此,我会有一个 video.php 页面,在此页面中,我可以提交视频的视频标题和播放该视频的人的名字.

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

现在,如果我要进入我的主页,并且看到此新帖子.如果我点击它.它将在标题中发送 video_id 信息,因此我可以从 video.php 中获取它.

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.

页面: 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'].
                 ';                   
    }
?>

现在我将如何显示与视频关联的视频名称和所有 cast_name .我使用的JOIN代码只能在表格中显示1个名称.有人知道我该如何获取其他剩余的姓名?

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秒登录
发送“验证码”获取 | 15天全站免登陆