php - How to JOIN 2 tables in MYSQL with same ID on multiple rows -
i'm having hard time figuring out. hope can me figure out.
for example. have video site , in video site can submit video posts name of people play in video. don't want add 1 name can more. i'd have video.php page , in page can submit video title of video , names of people 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 on homepage , see new post. if click on it. send video_id information in header can fetch video.php.
page: video.php
<?php $id = $_get['id']; $result = mysql_query (" select * post left join cast on post.video_cast = cast.cast_video video_id='$id' "); while($row = mysql_fetch_array($result)){ echo ' <h1>'.$row['video_name'].'</h1> starring: '.$row['cast_name']. '; } ?>
now how able show video_name , cast_name associated video. join code use can show 1 name out of table. know how fetch other remaining name(s)?
you can list names in 1 row group_concat()
select video_name, group_concat(cast_name) cast post left join cast on post.video_cast = cast.cast_video video_id='$id' group video_name
Comments
Post a Comment