简体   繁体   English

如何在Mysql中选择包括给定日期在内的日期范围之间的记录

[英]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. 我需要检查from_date和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. 例)员工申请2012年11月8日至2012年11月9日的假期,这里我需要检查2012年11月8日的日期范围,其中包括from_date和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. 当我使用Mysql BETWEEN时,它将仅检查范围内的日期,并且不包括from_date和to_date。

use a comparison operator: 使用比较运算符:

WHERE from_date >= '2012-11-08' AND to_date <= '2012-11-08' from_date > ='2012-11-08'和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. MySQL中的BETWEEN是包含性的,这意味着2012-11-08属于2012-11-08-2012-11-09的范围。 Your problem is probably the TIME part. 您的问题可能是“ TIME”部分。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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