两个值desc之间的SQL查询

时间:2012-04-24 17:18:00

标签: mysql sql

我有一个MySQL表,其中有两个字段start_intervalend_interval都存储了表示小时数的int个值。我正在使用此查询从该表中选择记录:

SELECT *
FROM messages
WHERE 23 BETWEEN start_interval AND end_interval;

如果start_interval大于end_interval,我如何获取值?例如start_interval = 22end_interval = 5

4 个答案:

答案 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)?