简体   繁体   English

我如何在MySQL中查询两个日期,其中datefrom在同一表的第二列中?

[英]How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table?

I have a hotel reservation system MySQL database which has a table 'bookings'. 我有一个酒店预订系统MySQL数据库,其中有一个表'bookings'。 The 'bookings' table includes two columns, datefrom (checkin) and dateto (checkout), their data-types are DATE. “预订”表包括两列,即datefrom(签入)和dateto(签出),其数据类型为DATE。 I need to query the database to show all bookings between two searchable dates. 我需要查询数据库以显示两个可搜索日期之间的所有预订。

The code I have is as follows: 我的代码如下:

SELECT * FROM bookings WHERE datefrom '2018-06-01' AND '2018-06-07';

This returns any booking where the start date exists between these two dates but I need to adapt the query to include bookings where the date starts before '2018-06-01' but includes the daterange shown in the query above. 这将返回开始日期介于这两个日期之间的所有预订,但我需要对查询进行调整,以包括日期开始于“ 2018-06-01”之前但包含上面查询中显示的日期范围的预订。 I understand the DATEDIFF function but I am not sure how/whether I can use that to return a date that, for example starts at '2018-05-23' and finishes '2018-06-02' (within the date range above). 我了解DATEDIFF函数,但不确定如何/是否可以使用它返回一个日期,例如,从“ 2018-05-23”开始并完成“ 2018-06-02”(在上述日期范围内) 。

I think you want this logic: 我认为您需要以下逻辑:

SELECT b.*
FROM bookings b
WHERE b.datefrom <= '2018-06-07' AND
      b.datefrom >= '2018-06-01'; 

This gives you any bookings that overlap with that period. 这将为您提供与该期间重叠的所有预订。

Following logic checks for overlapping date ranges: 以下逻辑检查日期范围是否重叠:

where start1 <= end2 and end2 >= start1

eg to find matching rows between '2018-06-01' and '2018-06-07' 例如在“ 2018-06-01”和“ 2018-06-07”之间找到匹配的行

WHERE datefrom <= '2018-06-07'
  AND dateto >= '2018-06-01'

Depending on your logic you might need to switch >= to > 根据您的逻辑,您可能需要将>=切换到>

暂无
暂无

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

相关问题 MySQL查询-一个表-一天之内找到两个日期-来自不同列+差异计算的同一实体 - Mysql query - one table - find two dates within one day - of the same entity from different column + diff calculations 如何从同一个表的两列查询等于一个值 - How to query from two column of a same table where equal to one value 如何使用mysql连接查询在一列中的两个位置? - how can use two where in one column with mysql join query? mysql在where子句中从同一表中选择两次具有不同日期的列 - mysql Select one column twice from the same table with different dates in the where clause 在只有一列作为查询基础的MySQL中如何做数据透视 - How to do Pivot in MySQL where only one column is the base of the query MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? 如何使用mysql基于同一个表的其他两列填充列? - how do I populate a column based on other two columns of the same table using mysql? MYSQL Join WHERE 两个日期列之间的两个日期列 - MYSQL Join WHERE Two dates column between two dates column 如何添加将一列分为两张的MySQL查询的结果? - How do I add the results of a MySQL query which splits a column into two to a table? 如何在MySQL的一个表的一列上进行搜索调用? - How do I do a search call on one column of a table in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM