Skip to content
Advertisement

Postgresql find by two columns

I have join table tags_videos

How can I select distinct video_id who have two specific tag_id

For example my tag_ids is 1195 and 1198, i should get video_ids 15033 and 15036 (who have 1195 and 1198 tag_id)

Advertisement

Answer

Extract the unique (tag_id, video_id) pairs for the two tags in t CTE and select these video_id‘s that have both tag_id‘s (i.e. 2 occurrences).

DB-fiddle demo

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement