在 SQL 查询中我无法获得真正的数据
In the SQL Query I couldn't get truly data
你好,
我有一个设施table。我想知道哪个 hotel_id 同时有 2 个设施。
SELECT *
from hotel_facilities_has
WHERE isHas='1' AND (facilities_id=1 AND facilities_id=2)
GROUP BY hotel_id
没有记录。
您的查询不起作用,因为您要查找同时满足两个 条件的行,而这是不可能发生的。您需要跨行查看同一组。
相反,您可以过滤满足任一条件的行,然后确保每组有两行:
select hotel_id
from mytable
where isHas = 1 and facilities_id in (1, 2) -- one or the other
group by hotel_id
having count(*) = 2 -- two rows in the group
如果 (hotel_id, facilities_id )
元组不唯一,则需要 having count(distinct facilities_id) = 2
。
你好, 我有一个设施table。我想知道哪个 hotel_id 同时有 2 个设施。
SELECT *
from hotel_facilities_has
WHERE isHas='1' AND (facilities_id=1 AND facilities_id=2)
GROUP BY hotel_id
没有记录。
您的查询不起作用,因为您要查找同时满足两个 条件的行,而这是不可能发生的。您需要跨行查看同一组。
相反,您可以过滤满足任一条件的行,然后确保每组有两行:
select hotel_id
from mytable
where isHas = 1 and facilities_id in (1, 2) -- one or the other
group by hotel_id
having count(*) = 2 -- two rows in the group
如果 (hotel_id, facilities_id )
元组不唯一,则需要 having count(distinct facilities_id) = 2
。