如果当前时间在行的开始和结束时间之间,则检索行

时间:2016-08-26 05:17:45

标签: mysql sql datetime

如果我运行查询

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name;

我得到以下结果

  

10:30:50 | 1:00:00 | 12:00:00

     

10:30:50 | 1:00:00 | 12:00:00

但是当我尝试检索行时,如果当前时间介于START_HOUR和END_HOUR(the START_HOUR and END_HOUR are INT(2)之间),则返回0行。

我尝试了以下查询但没有工作

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name WHERE curtime() >=concat(START_HOUR,':00:00') && curtime()<=concat(END_HOUR,':00:00');

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name WHERE curtime() BETWEEN concat(START_HOUR,':00:00') AND concat(END_HOUR,':00:00');

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name WHERE concat(curtime(),'') >=concat(START_HOUR,':00:00') && concat(curtime(),'')<=concat(END_HOUR,':00:00');

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name WHERE concat(curtime(),'') BETWEEN concat(START_HOUR,':00:00') AND concat(END_HOUR,':00:00');

SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from table_name WHERE CONCAT(DATE_FORMAT(NOW(), '%H:%i'), ':00') >=concat(START_HOUR,':00:00') && CONCAT(DATE_FORMAT(NOW(), '%H:%i'), ':00')<=concat(END_HOUR,':00:00');

以及这些的所有组合。

请帮我找出我做错了什么,以及如何找回所需的记录。

3 个答案:

答案 0 :(得分:1)

您需要将CAST时间值转换为TIME类型。

SELECT 
    curtime(), 
    CAST(concat(START_HOUR,':00:00') as TIME), 
    CAST(concat(END_HOUR,':00:00') as TIME)
FROM table_name WHERE 
    CURTIME() BETWEEN 
        CAST(CONCAT(START_HOUR,":00:00") AS TIME) AND 
        CAST(CONCAT(END_HOUR,":00:00") AS TIME)

答案 1 :(得分:1)

当您与curtime()进行比较时,请尝试将您的字符串作为时间首先进行CAST。

请尝试以下操作:

SELECT colums FROM table WHERE curtime() < CAST(concate(START_HOUR,'00:00') AS time) AND curtime() > CAST(concate(END_HOUR,'00:00') AS time);

答案 2 :(得分:1)

您只需比较小时数

即可
SELECT curtime(), concat(START_HOUR,':00:00'), concat(END_HOUR,':00:00') from oknot_ad_agreements 
WHERE HOUR(curtime()) >= START_HOUR && HOUR(curtime()) <= END_HOUR;