I have 3 tables. An "item" table and a "tag" table. Then I also have an "item_tag" table which ties the 2 together.
I want to make a query that lists all the items that have particular tags assigned to it. So I would like the query to list all items that have tag x and tag y applied to it.
This is what I have come up with so far.. except that this will list any that match either tag id 148 or tag id 152. If I make it say "AND" it shows no results.
SELECT *
FROM (`item`)
RIGHT OUTER JOIN `item_tag` ON `item`.`id` = `item_tag`.`fk_item_id`
WHERE `item_tag`.`fk_tag_id` = "152" OR `item_tag`.`fk_tag_id` = "148"
GROUP BY `item`.`id`
You can get ids of the items that have all of the tags you want using this query:
And then just
You just have to modify the ids and the
3which is the count of those ids.When your table does not have
UNIQUEconstraint (it should have) and there can be the same tags in particular item you should modify the query to this: