I have a keywords and a categories table with an ID and a name col (I can’t merge them into a single table!)
There’s an articles table with ID, name, text.
And the many-to-many connection tables:
-
ckw for keywords, columns: key_id, article_id
-
ccat for categories, columns: cat_id, article_id
I can write a query to get those articles which belong to keywords ID 5 AND ID 7:
SELECT articles.name FROM articles
JOIN ckw ON (ckw.article_id = articles.id)
WHERE ckw.key_id IN (5, 7)
GROUP BY ckw.article_id
HAVING COUNT(ckw.key_id) = 2
But what if I want to get those articles which belong to keywords ID 5 AND ID 7, and also belong to category ID 12 AND ID 18? I can’t figure the right query out.
1 Answer