我有一个MySQL表,其中有两个字段start_interval
和end_interval
都存储了表示小时数的int
个值。我正在使用此查询从该表中选择记录:
SELECT *
FROM messages
WHERE 23 BETWEEN start_interval AND end_interval;
如果start_interval
大于end_interval
,我如何获取值?例如start_interval = 22
和end_interval = 5
?
答案 0 :(得分:7)
怎么样:
SELECT * FROM messages
WHERE 23 BETWEEN LEAST(start_interval, end_interval) AND
GREATEST(start_interval, end_interval);
答案 1 :(得分:2)
也许你检查两个方向:
SELECT * FROM messages
WHERE
(23 BETWEEN start_interval AND end_interval) OR
(23 BETWEEN end_interval AND start_interval);
答案 2 :(得分:0)
…
WHERE 23 BETWEEN start_interval AND end_interval
OR start_interval > end_interval AND
23 NOT BETWEEN end_interval + 1 AND start_interval - 1
答案 3 :(得分:0)
怎么样:
where 23 between start_interval and if(start_interval>=end_interval,end_interval+24,end_interval)?