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