sql选择对象及其所有可能的引用

1rhkuytd  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(251)

我有三张table:
结构(id整数,名称文本)
属性(id整数,名称文本)
以及一个引用织物特性的表
属性到结构(结构id,属性id,唯一(结构id,属性id))
现在我需要提出一个sql请求,它将选择具有所有现有属性的所有结构。
我已经尝试过的是:

select 
    fabric_id, fabric_name 
from 
    properties_to_fabric 
where
    count(select * from properties_to_fabric where fabric_id = fabric_id) = count(select * from property);

不过,这不管用,我也没有其他想法。
如果数据库很重要,那么它就是sqlite3

lc8prwob

lc8prwob1#

可以使用聚合:

select pf.fabric_id
from properties_to_fabric pf
group by pf.fabrid_id
having count(*) = (select count(*) from properties);

笔记:
如果你需要名字,你可以 joinfabrics table。
如果可以为给定的结构复制属性,则使用 having count(distinct property_id) = . . . .

tktrz96b

tktrz96b2#

您可以在不存在的情况下执行此操作:

select f.* from fabric f
where not exists (
  select 1 from property p
  where (f.id, p.id) not in (select fabric_id, prop_id from properties_to_fabric)
)

请参阅简化的演示。

相关问题