php - MySQL how to grab more information with JOIN? -


currently, have 2 mysql tables.

first table stores relation between friend , picture.

table 1

 id | pic_id | friend_id ---------------------------- 0 | 123 | 84589 1 | 290 | 11390 2 | 884 | 84589 3 | 456 | 84589 4 | 123 | 11111 5 | 456 | 22222 

table 2

second table stores more information pic...

id | pic_id | title | color | detail ---------------------------------------------- 0 | 123 | hello | black | brush 1 | 124 | world | red | paint 2 | 884 | sample | green | star 

i use join grab information need...

but if want use pic_id's matched above sql , find other friend_id's same pic_id?

for example, above sql command give me rows 0, 2, , 3 pic_id's 123, 884, 456. sql command should use loop through these pic_id's , grab associated friend_id's? want end 11111 pic_id 123 , 22222 pic_id 456.

i have used following code accomplish above. not efficient , slow though.

$sql = "select b.title, b.color, b.detail table1 inner join table2 b on a.pic_id = b.pic_id friend_id = 84589"; $res = mysqli_query($link,$sql); if($res){ while ($rec = mysqli_fetch_assoc($res)){ $pic_id.=$rec['pic_id'].","; $arr[] = $rec; } } $each_id = explode(',',$pic_id); foreach($each_id $key => $value){ if ($value){ $next_sql = "select friend_id table1 pic_id=".$value; $next_res = mysqli_query($link,$next_sql); if ($next_res){ while($next_rec = mysqli_fetch_assoc($next_res)){ //do things } } } } 

sorry if unclear. please let me know , clarify.

all appreciated!

try (updated) :

select a.friend_id, a.pic_id table1 exists (select 1 table1 t t.pic_id = a.pic_id , t.friend_id = 84589) 

check - http://sqlfiddle.com/#!3/91cdf/3


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -