我有如下查询
SELECT * FROM programs where startTime between now() and now() + INTERVAL 1 DAY;
是否可以在 MySQL 中使用从现在到今天午夜之间的时间编写查询?
像下面的东西
SELECT * FROM programs where startTime between now() and now() + midnight 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;
我建议您始终使用 clopen(封闭-开放)间隔进行日期和时间比较。BETWEEN
表示封闭的间隔(从两侧)。@Aaron Bertrand 的博文中有一个很好的解释: BETWEEN 和魔鬼有什么共同点?. 这是编写查询的一种方法:
SELECT *
FROM programs
WHERE startTime >= NOW()
AND startTime < CURRENT_DATE() + INTERVAL 1 DAY ;
SELECT * FROM programs where startTime between now() and CURRENT_DATE() + INTERVAL 1 DAY;
当前日期返回一天的开始,然后我们添加 1 天以到达它的结束。
您可以尝试以下方法:虽然可能很长。
样品表:
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
SELECT *
FROM programs
WHERE DATE(startTime) = CURRENT_DATE AND startTime > CURRENT_TIMESTAMP
尝试这个 ::
SELECT * FROM programs where startTime between now() and DATE_SUB(CURDATE(), INTERVAL 1 DAY)