简体   繁体   English

在mysql php中加入2个表的查询

[英]join query for 2 tables in mysql php

i have two table in database like following 我在数据库中有两个表,如下所示

table 1 : playlist 表1:播放列表

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

table 2 : playlist_shared 表2:playlist_shared

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

now i want to select unique id and name of playlist from playlist table where user_id=3 in both table. 现在,我想从两个列表中的user_id=3的播放列表表中选择unique id和播放列表name

output will be 输出将是

 id    name
 1     test1
 2     test2
 3     test3

please help me to short out this. 请帮助我简化这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM