2015-03-19 43 views
0

我有兩個表中的數據庫就像下面連接查詢在MySQL的PHP​​ 2個表

表1:播放列表

id name user_id 
1  test1 3 
2  test2 4 
3  test3 3 

表2:playlist_shared

id playlist_id user_id 
1  1    6 
2  1    3 
3  2    3 

現在我想從播放列表表格wh選擇播放列表的unique idname在這兩個表中都有user_id=3

輸出將

id name 
1  test1 
2  test2 
3  test3 

請幫我短路這樣。

+0

'選擇ID,從播放列表名稱'這會給你一個預期輸出 – 2015-03-19 05:30:06

回答

0
select id,name from playlist where user_id = 3 
union 
select p.id id, name 
    from (select * from playlist_shared where user_id=3) ps join playlist p 
    on p.id = ps.playlist_id 
0
SELECT pl.id, pl.`name` FROM playlist_shared ps 
INNER JOIN playlist pl ON pl.id = ps.playlist_id 
WHERE ps.user_id = 3 
GROUP BY ps.playlist_id