简体   繁体   English

2个日期之间的MySQL日期搜索返回0行

[英]Mysql date search between 2 dates returns 0 rows

Basically i want to get the people who checked in after given date and checked out before or on the same day of the check out date. 基本上,我想让那些在给定日期之后签入并在签出日期之前或当天签出的人。 Here are some queries i have tried. 这是我尝试过的一些查询。

SELECT * FROM `booking`
        WHERE (check_in BETWEEN '2017-09-10' AND '2017-09-21')
          AND (check_out between '2017-09-10' and '2017-09-21')

which returns 0 rows and 返回0行

SELECT * FROM `booking`
        WHERE check_in >= '2017-09-10' AND check_out <= '2017-09-21'

returns 0 rows. 返回0行。 I have a customer check in on 2017-09-18 and check out on 2017-09-21. 我有一个客户在2017-09-18签入并在2017-09-21签出。 How to resolve this ? 如何解决呢?

I don't know the values in your DB but a common mistake that you may have made is assume that your database 2017-09-21 entry will fit into (check_out between '2017-09-10' and '2017-09-21') . 我不知道您数据库中的值,但您可能犯的一个常见错误是假定您的数据库2017-09-21条目将适合(check_out between '2017-09-10' and '2017-09-21') Your 2017-09-21 is in fact 2017-09-21 00:00:00. 您的2017-09-21实际上是2017-09-21 00:00:00。 Most probably your database date has some non-zero hours and minutes to it which makes it LARGER than 2017-09-21 00:00:00. 您的数据库日期很可能具有一些非零小时和分钟,这使其比2017-09-21 00:00:00大。

For eg 2017-09-21 05:06:23 is larger than 2017-09-21 00:00:00 and will not fit into your condition. 例如,2017-09-21 05:06:23大于2017-09-21 00:00:00,将不适合您的情况。

Try: 尝试:

SELECT * FROM `booking`
        WHERE (check_in >= '2017-09-10' AND check_in < '2017-09-22')
          AND (check_out >= '2017-09-10' AND check_out <'2017-09-22')

Try like this: 尝试这样:

SELECT * FROM `booking`
        WHERE DATE(check_in) >= '2017-09-10' AND DATE(check_out) <= '2017-09-21'

Use mysql date format function in check in and check out field 在签入和签出字段中使用mysql日期格式功能

SELECT * FROM `booking`
WHERE DATE_FORMAT(check_in,'%Y-%m-%d') >= '2017-09-10' AND 
DATE_FORMAT(check_out,'%Y-%m-%d') <= '2017-09-21'

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

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