大家好,我的表格中有下面一行作为示例
id shop_id start_time end_time
1 21 10:00 11:00并且我想检查表中是否存在start_time和end_time我正在使用以下查询但不能正确工作,我哪里错了?
select *
from usr_booking
where shop_id='21'
and start_time between '10:10:00' and '11:01:00'
or end_time between '10:10:00' and '11:01:00'发布于 2019-01-19 14:40:28
您需要清楚地将shop_id和时间范围上的检查分开:
SELECT *
FROM usr_booking
WHERE
shop_id = 21 AND
(start_time BETWEEN '10:10:00' AND '11:01:00' OR
end_time BETWEEN '10:10:00' AND '11:01:00');MySQL中的AND运算符比OR运算符具有更高的优先级。因此,您当前的查询实际上是这样计算的:
SELECT *
FROM usr_booking
WHERE
(shop_id = 21 AND
start_time BETWEEN '10:10:00' AND '11:01:00') OR
end_time BETWEEN '10:10:00' AND '11:01:00';显然,这与你可能想要的逻辑不同。
发布于 2019-01-19 14:36:35
尝试在这种类型的查询中进行分组
select * from usr_booking where shop_id='21' AND ((start_time between '10:10:00' and '11:01:00') OR (end_time between '10:10:00' and '11:01:00'))添加额外的括号make groups。
发布于 2019-01-19 14:39:22
尝试此查询:
SELECT *
FROM usr_booking
Where shop_id='21' AND start_time BETWEEN '10:10:00' AND '11:01:00'
AND end_time BETWEEN '10:10:00' AND '11:01:00'https://stackoverflow.com/questions/54264668
复制相似问题