I am working with nodejs and postgresql. My postgresql relation has 3 columns:
- id
- lesson_id
- tag_id.
A lesson could belong to 1 or multiple tags.
I am trying to select all the lesson whose belongs to the requested tags.
For example
- tags requested are id 10 and 2, the query should response with lesson id = 3
- tags requested are id 1 and 17, the query should response with lesson id = 6
- tag requested is 3, the query should response with lessons id 1, 2, 4
I have tried some sql queries like this one:
const selectLessonByTag = await pgClient.query(
`SELECT DISTINCT ON (lesson_id)
lesson_id FROM "lesson_has_tag"
WHERE tag_id = $1 AND tag_id = $2
GROUP BY lesson_id
ORDER BY lesson_id`,
[2,10]);
but it's not the expected answer.