第一张表
属性
id Name
1 ABC
2 XYZ
3 GHQ
Property_options
id property_id option
1 1 terrace
2 1 balcony
3 1 garaj
1 2 terrace
2 2 balcony
3 2 garaj
我想使用三个选项过滤属性(terrace
,balcony
和garaj
)
如果用户检查三个选项,那么只有那些属性会有三个选项,而不是两个或一个。
答案 0 :(得分:0)
试试这个:
select Name from Property where id in (select property_id from Property_Options where Property_Option = 'terrace ' and Property_Option = 'balcony ' and Property_Option = 'garaj')a
答案 1 :(得分:0)
我会使用聚合和group by
:
select p.propertyid
from property p
group by p.propertyid
having sum(property_option = 'terrace') > 0 and
sum(property_option = 'balcony') > 0 and
sum(property_option = 'garaj') > 0;
每个条件都会计算属性出现的次数。这是一种灵活的方法。如果你想要前两个,而不是“garaj”,你会使用:
having sum(property_option = 'terrace') > 0 and
sum(property_option = 'balcony') > 0 and
sum(property_option = 'garaj') = 0;
答案 2 :(得分:0)
这是确切的答案
从属性p选择p.id,p.name INNER JOIN property_option po on p.id = po.property_id group by p.id 总和(po.meta_name ='terrace')> 0和sum(po.meta_name ='balcony')> 0和sum(po.meta_name ='garaj')> 0