I have table containing products, a table containing tags for the products where each product can have several tags, eg
products
product_id|name|…
1 |book
2 |game
3 |desk
…
,
tags
product_id|tag
3 |used
3 |physical
Is there a “nice” way using only SQL to query for all products which have all given tags where the tags are in an array only known at compile time?
The only way I can think of is programmatically generating a query with multiple JOIN
s on tags
, renaming to tag1
, tag2
and so on and then filtering in the WHERE
clause with tag1 = tags_array[1] AND tag2 = tags_array[2]
and so on.
I am using SQLITE, if this is relevant.