在我的mysql查询中,有一堆带有一个OR语句的AND条件。但我需要修改我的代码,使得我的两个给定日期中的任何一个在t2.start_id和t2.end_id之间,并且仍然与其他AND语句进行AND运算。
现在,我的OR语句执行并且没有检查其他AND语句
SELECT *
FROM med_files t1 Inner Join med_users t2 ON t1.med_id = t2.id
WHERE t1.med_id in (54810,54812,54813,54811) AND t2.user_id <> 6 AND
'2014-07-16 00:00:00' between t2.start_id AND t2.end_id OR
'2014-07-17 09:54:00' between t2.start_id AND t2.end_id'
答案 0 :(得分:0)
使用括号来描述两个检查中的每个检查之间的界限,如下所示:
AND t2.user_id <> 6
AND --Either of the two should be true
(
('2014-07-16 00:00:00' between t2.start_id AND t2.end_id)
OR
('2014-07-17 09:54:00' between t2.start_id AND t2.end_id)
)
答案 1 :(得分:0)
您必须使用括号来分隔AND
&amp; OR
试试这个
SELECT *
FROM med_files t1 INNER JOIN med_users t2 ON t1.med_id = t2.id
WHERE t1.med_id IN (54810,54812,54813,54811) AND t2.user_id <> 6 AND
(('2014-07-16 00:00:00' between t2.start_id AND t2.end_id) OR
('2014-07-17 09:54:00' between t2.start_id AND t2.end_id))
答案 2 :(得分:0)
括号将完成工作,他们控制你的AND和OR的优先级,并将它们组合在一起:
where
t1.med_id in (54810,54812,54813,54811)
AND t2.user_id <> 6
AND ('2014-07-16 00:00:00' between t2.start_id AND t2.end_id
OR '2014-07-17 09:54:00' between t2.start_id AND t2.end_id')