So I have this:
(CURDATE() BETWEEN start_date AND end_date)
Works fine.
But when the CURDATE()
is 2011-12-02 and the end_date
is 2011-12-02 will it grab the row?
Eg my start_date is 2011-12-01 00:00:00 and my end date is 2011-12-02 23:59:59
So it only works when the date is between but not if it's ON the end_date
itself.
Or maybe it should check for the time too, because it still needs to be selected with this query when it's 2011-12-02 15:30:00 for example.
How can I do this?
好吧,你可以试试
CURDATE() BETWEEN start_date AND DATE_ADD(end_date, INTERVAL 1 DAY)
Maybe the answer to this question refers to a bug in an old version of MySql because between
is inclusive , which means it will grab rows between the start and end dates inclusive, not just between the start and one day before the end.
Try this:
SELECT CURDATE() BETWEEN CURDATE() AND CURDATE();
The result is 1
(ie true
). I believe the original poster problem lies with mixing up proper dates ( DATE
) and dates with time ( DATETIME
or TIMESTAMP
).
Try this:
SELECT NOW() BETWEEN CURDATE() AND CURDATE();
SELECT NOW() BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 DAY);
The result is 0
for the first select and 1
for the second. What happened is a DATE
is equivalent to a DATETIME
with zero time so unless NOW()
is called exactly at midnight it will be greater than CURDATE()
and fall outside of the BETWEEN
statement. To prevent this test only the DATE
part of a DATETIME
using the DATE()
function:
SELECT DATE(NOW()) BETWEEN CURDATE() AND CURDATE();
Since both columns are timestamps, you need to make sure times don't trip you up. To keep the times from tripping you up, cast the timestamps to date.
where current_date between cast(start_date as date)
and cast(end_date as date);
使用start_date <= CURDATE() AND end_date > CURDATE()
It will work ... BETWEEN
works inclusive of the boundary values. That is,
(CURDATE() BETWEEN start_date AND end_date)
including start_date,end_date and any day falling between
CURDATE() BETWEEN start_date AND ADDDATE(CURDATE(), INTERVAL 1 DAY);
cast(end_date - Start_date为双精度)* 86400
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.