dan*_*ang 2 mysql optimization
我有一个具有以下结构的表:
id  | property_id | location_type
1   | 1           | 1
2   | 1           | 2
3   | 2           | 1
4   | 3           | 2
5   | 4           | 1
6   | 4           | 2
id - 是表的主键.property_id是我的数据库(外键)的属性ID.location_type是海滩(值-1),山(值-2)
你能帮助我获取SQL查询来选择property_id,其中location_type = 1 AND location_type = 2即属性有海滩和山脉.
我有很多过滤器(大约9种类型的location_type和其他过滤器).我正在创建一个带过滤器的属性搜索引擎.请帮助获得最优化的查询,以便减少加载时间.
select
property_id
from table
where location_type in (1,2)
group by property_id
having count(distinct(location_type)) = 2
如果您没有重复项,则可以删除distinct子句.