简体   繁体   中英

How to select the record between the date range including the given date in Mysql

I need to check the date in the range from_date and to_date.

Ex) Employee applies leave from 2012-11-08 to 2012-11-09, here I need to check 2012-11-08 with the date range includes the from_date and to_date.

Please suggest me to use the proper queries to get the records.

When I use Mysql BETWEEN, it checks the date in between range only and not including the from_date and to_date.

use a comparison operator:

WHERE from_date >= '2012-11-08' AND to_date <= '2012-11-08'

BETWEEN in MySQL is inclusive, which means that 2012-11-08 falls within the range of 2012-11-08 - 2012-11-09. Your problem is probably the TIME part.

Try casting the database fields to date in your query and see what result you get, ie:

SELECT x FROM y WHERE '2012-11-08' BETWEEN DATE(date_from) AND DATE(date_until)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM