[英]Postgresql find by two columns
我有加入表 tags_videos
tag_id | video_id
--------+----------
1195 | 15033
1198 | 15033
1199 | 15033
1196 | 15034
1198 | 15034
1199 | 15034
1197 | 15035
1198 | 15035
1199 | 15035
1195 | 15036
1197 | 15036
1198 | 15036
如何選擇具有兩個特定tag_id
的不同video_id
例如我的 tag_ids 是1195 and 1198
,我應該得到 video_ids 15033 and 15036
(他們有 1195 和 1198 tag_id)
為t
CTE 中的兩個標簽提取唯一的 (tag_id, video_id) 對,並選擇具有兩個tag_id
的這些video_id
(即出現 2 次)。
with t as
(
select distinct tag_id, video_id
from tags_videos
where tag_id in ('1195', '1198')
)
select video_id from t
group by video_id having count(*) = 2;
DB-fiddle演示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.