MySQL Join存在问题,需要满足多个条件 [英] Having issues with a MySQL Join that needs to meet multiple conditions

查看:312
本文介绍了MySQL Join存在问题,需要满足多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌子roomsrooms facilities,我必须选择具有所需设施的房间.

I have two tables rooms and rooms facilities and I have to select the rooms with desired facilities.

如果我选择的房间只有一种设施(id = 4的设施-id_fu-).使用以下查询,一切正常:

If I select a room with one facility (facility with id=4 - id_fu - ). using the following query Everything it's ok:

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 

但是,如果我想选择具有更多设施的房间,则假设id = 4和id = 3的设施..使用以下查询无效:

But if I want to select the room with more facilities, let's say facilities with id=4, and id=3 ..using the following query it doesn't work:

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 

我不明白为什么它不起作用,但是我无法弄清楚如何处理这种情况.

I don't understand why it doesn't work, but I can't figure up how to put the condition.

推荐答案

您可以用括号将条件分组.当您检查一个字段是否等于另一个字段时,您想使用OR.例如WHERE a='1' AND (b='123' OR b='234').

You can group conditions with parentheses. When you are checking if a field is equal to another, you want to use OR. For example 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

这篇关于MySQL Join存在问题,需要满足多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆