Given a table of items, a table of tags and a join table between them, what is a good and efficient way to implement queries of the form:
p1 AND p2 AND ... AND pn AND NOT n1 AND NOT n2 ... AND NOT nk
I am using SQL. So to find all items that match all tags p1…pn and none of n1…nk?
Is there a good ‘standard’ solution for this?
Difficult to say without knowing your schema, but something like this would work:
inner join to tags that are to be included, and do an anti-join (outer join + where a required column is null) to tags that are not to be included