Sqlite 如何查询包含给定多对多关系的所有值的所有行?(不仅仅是其中一个!)
我有以下数据库结构:Sqlite 如何查询包含给定多对多关系的所有值的所有行?(不仅仅是其中一个!),sqlite,relational-division,Sqlite,Relational Division,我有以下数据库结构: 表1:家具(字段:ID) 表2:材料(字段:ID) 表3:材料匹配:(字段:ID、FK_家具、FK_材料) 这种结构能够将许多材料与每件家具联系起来 我知道如何查询所有家具,这些家具有一些特殊的材料,材料之间的逻辑关系是或: select distinct(furniture.ID) from Furniture left join MaterialMatching ON MaterialMatching.FK_Furniture = Furniture.ID
- 表1:家具(字段:ID)
- 表2:材料(字段:ID)
- 表3:材料匹配:(字段:ID、FK_家具、FK_材料)
select distinct(furniture.ID) from Furniture
left join MaterialMatching ON MaterialMatching.FK_Furniture = Furniture.ID
left join Material On Material.ID = MaterialMatching.FK_Material
where Material.ID IN (<< material ids I want to query>>)
从家具中选择不同的(furniture.ID)
MaterialMatch.FK_Furniture=Furniture.ID上的左连接MaterialMatch
Material.ID=MaterialMatching.FK_Material上的左连接材质
其中Material.ID位于(>)
问题
如何查询包含所有材质的所有家具?我想买所有家具,有材料1和材料2和…这是一个相当肮脏的孤寂,但它应该解决它。它将归还所有指定了所有材料的家具
select f.*
from Furniture f
left join (select ftemp.id, count(distinct(mm.FK_Material)) matcount
from Furniture ftemp
left join MaterialMatching mm on mm.FK_Furniture = ftemp.ID
group by ftemp.id) mmtemp on f.id = mmtemp.id
where mmtemp.matcount = (select count(1) from Material)
事实上,我想知道这两个。。。但我将其更改为SQLite作为主要SQL系统…您可能添加了标记关系分区。。。有了它,我可以找到一个解决方案。。。