MySQLjoin多个条件

我有一个SQL查询的问题,实际上是一个简单的查询,但我不知道我错过了什么,所以我来问你的帮助…所以,我必须做的

我有两个桌子roomsrooms facilities …我必须select所需的设施的房间..

如果我select一个房间有一个设施(设施id = 4 – id_fu – )…使用以下查询

 SELECT u.* FROM rooms u JOIN facilities_r fu ON fu.id_uc = u.id_uc AND fu.id_fu = '4' WHERE 1 AND vizibility='1' GROUP BY id_uc ORDER BY u_premium desc, id_uc DESC 

一切安好。

但是,如果我想select更多设施的房间,让我们说设施id = 4,和id = 3 ..使用以下查询

 SELECT u.* FROM room u JOIN facilities_r fu ON fu.id_uc=u.id_uc AND fu.id_fu = '4' AND fu.id_fu = '3' WHERE 1 AND vizibility = '1' GROUP BY id_uc ORDER BY u_premium DESC, id_uc DESC 

它不工作。

我不明白为什么它不起作用,但我不知道如何把条件…

谢谢,米海

您可以用圆括号分组条件。 当你检查一个字段是否与另一个字段相同时,你想使用OR 。 例如WHERE a='1' AND (b='123' OR b='234')

 SELECT u.* FROM rooms AS u JOIN facilities_r AS fu ON fu.id_uc = u.id_uc AND (fu.id_fu='4' OR fu.id_fu='3') WHERE vizibility='1' GROUP BY id_uc ORDER BY u_premium desc, id_uc desc 
 SELECT u . * FROM room u JOIN facilities_r fu ON fu.id_uc = u.id_uc AND (fu.id_fu = '4' OR fu.id_fu = '3') WHERE 1 and vizibility = '1' GROUP BY id_uc ORDER BY u_premium desc , id_uc desc 

你必须在这里使用OR,而不是AND。

由于id_fu不能一次等于4和3。

如果你join设施表两次,你会得到你在之后:

 select u.* from room u JOIN facilities_r fu1 on fu1.id_uc = u.id_uc and fu1.id_fu = '4' JOIN facilities_r fu2 on fu2.id_uc = u.id_uc and fu2.id_fu = '3' where 1 and vizibility='1' group by id_uc order by u_premium desc, id_uc desc