0

我有一个 MySQL 表,它有两个字段start_interval,并且end_interval都存储int代表小时的值。我正在使用此查询从该表中选择记录:

SELECT *
FROM messages
WHERE 23 BETWEEN start_interval AND end_interval;

start_interval当大于时如何获取值end_interval?例如 ifstart_interval = 22end_interval = 5?

4

4 回答 4

7

怎么样:

SELECT * FROM messages
WHERE 23 BETWEEN LEAST(start_interval, end_interval) AND
                GREATEST(start_interval, end_interval); 
于 2012-04-24T17:21:37.907 回答
2

也许你检查两个方向:

SELECT * FROM messages
WHERE
  (23 BETWEEN start_interval AND end_interval) OR
  (23 BETWEEN end_interval AND start_interval); 
于 2012-04-24T17:21:34.143 回答
0

怎么样:

where 23 between start_interval and if(start_interval>=end_interval,end_interval+24,end_interval)?
于 2012-04-25T19:29:57.320 回答
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
于 2012-04-25T18:17:05.763 回答