SQLite:标记和产品交叉点
我是SQLite新手,正在寻找创建查询的方法,以执行以下操作: 我有三张桌子:SQLite:标记和产品交叉点,sql,database,sqlite,Sql,Database,Sqlite,我是SQLite新手,正在寻找创建查询的方法,以执行以下操作: 我有三张桌子: goods: List of goods tags: list of tags tag_goods: table used to associate a tag to a product 这是每个表的结构: 货物: id (INTEGER, autoincrement) name (TEXT, name of the product) 标签: 标记货物: tag_id (INTEGER) goods_id (IN
goods: List of goods
tags: list of tags
tag_goods: table used to associate a tag to a product
这是每个表的结构:
货物:
id (INTEGER, autoincrement)
name (TEXT, name of the product)
标签:
标记货物:
tag_id (INTEGER)
goods_id (INTEGER)
UNIQUE (tag_id, goods_id)
我想要的是:
仅获取带有所有可能标签的产品的ID和名称(注意:我需要引用查询中的所有标签,而不是逐个引用)
(例如,意思是我只有四个标签:“黄色”、“圆形”、“可食”、“有机”并且只有商品“奶酪”和“甜瓜”与它们匹配)
以我有限的经验,我知道我可以尝试像intersect这样的方法,但当标签太多时,这是不切实际的。此查询:
select goods_id
from tag_goods
group by goods_id
having count(*) = (select count(*) from tags)
返回商品的所有id
s,这些商品具有tags
中的所有标签您可以使用它返回表
货物中的行,操作员在
中:
select * from goods
where id in (
select goods_id
from tag_goods
group by goods_id
having count(*) = (select count(*) from tags)
)
select * from goods
where id in (
select goods_id
from tag_goods
group by goods_id
having count(*) = (select count(*) from tags)
)