12

我有如下查询

SELECT * FROM programs where startTime between now() and now() + INTERVAL 1 DAY;

是否可以在 MySQL 中使用从现在到今天午夜之间的时间编写查询?

像下面的东西

SELECT * FROM programs where startTime between now() and now() + midnight 12;
4

5 回答 5

15

我建议您始终使用 clopen(封闭-开放)间隔进行日期和时间比较。BETWEEN表示封闭的间隔(从两侧)。@Aaron Bertrand 的博文中有一个很好的解释: BETWEEN 和魔鬼有什么共同点?. 这是编写查询的一种方法:

SELECT * 
FROM programs 
WHERE startTime >= NOW() 
  AND startTime < CURRENT_DATE() + INTERVAL 1 DAY ;
于 2012-12-06T08:07:17.907 回答
5
SELECT * FROM programs where startTime between now() and CURRENT_DATE() + INTERVAL 1 DAY;

当前日期返回一天的开始,然后我们添加 1 天以到达它的结束。

于 2012-12-06T07:28:15.560 回答
0

您可以尝试以下方法:虽然可能很长。

样品表:

ID  STIME                               ETIME
1   December, 05 2012 05:23:00+0000     December, 05 2012 07:30:00+0000
2   December, 05 2012 10:23:00+0000     December, 05 2012 12:30:00+0000
3   December, 06 2012 22:45:00+0000     December, 07 2012 01:00:00+0000
4   December, 06 2012 22:23:00+0000     December, 06 2012 23:55:00+0000
5   December, 06 2012 20:23:00+0000     December, 06 2012 22:55:00+0000
6   December, 07 2012 10:23:00+0000     December, 07 2012 12:30:00+0000

询问

SELECT  *,curtime() FROM prog
WHERE date(stime) = date(now())
AND date(etime) = date(now())
AND time_to_sec(SUBTIME(etime,'24:00:00')) >= 0
;

结果

ID  STIME                               ETIME   
4   December, 06 2012 22:23:00+0000     December, 06 2012 23:55:00+0000     
5   December, 06 2012 20:23:00+0000     December, 06 2012 22:55:00+0000     
于 2012-12-06T08:02:56.970 回答
0
SELECT *
FROM   programs
WHERE  DATE(startTime) = CURRENT_DATE AND startTime > CURRENT_TIMESTAMP
于 2012-12-06T09:09:31.670 回答
0

尝试这个 ::

SELECT * FROM programs where startTime between now() and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
于 2012-12-06T07:30:00.750 回答