Skip to content
Advertisement

PostgreSQL query get all entries with many-to-many relationship with another table

I have a table posts which has a many-to-many relationship with tags, the pivot table is called posts-tags.

I want to be able to retrieve all posts by a list of tag id’s.

Imagine

With tag id’s [1, 2, 3], I should get back [{id: 1, text: "foo"}]

With tag id’s [1], I should get back [{id: 1, text: "foo"}, {id: 2, text: "bar"}, {id: 3, text: "baz"}]

Basically, I want to retrieve all the posts related to the list of tags.

Advertisement

Answer

You can use a subquery to filter posts that have all the specified tags:

See fiddle.

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