2つのタグのいずれかが付いたアイテムが必要な場合は、次のようにします。
select distinct item_id, item_name
from items_tags
where tag_name in ('yellow', 'fruit');
両方のタグが付いたアイテムが必要な場合:
select item_id, item_name
from items_tags
where tag_name in ('yellow', 'fruit')
group by item_id, item_name
having count(*) = 2;
コメントに基づく
select a.id, a.item
from items a, items_tags b, tags c
where a.id = b.item_id
and b.tag_id = c.id
group by id, item
having (group_concat(c.tag) like '%yellow%'
and group_concat(c.tag) like '%fruit%')
or group_concat(c.tag) = 'red';
このクエリは、itemsテーブルからidとitemを提供します。黄色とフルーツの両方のタグが付いたアイテムを提供します。と赤いタグのみのアイテム。
2つのタグと2つのタグのみを持つアイテムを取得する場合は、次の条件を使用して句を作成します
(group_concat(c.tag) like '%yellow%'
and group_concat(c.tag) like '%fruit%'
and count(*) = 2)