【问题标题】:Select date range between bookings选择预订之间的日期范围
【发布时间】:2015-05-18 14:06:21
【问题描述】:

我有一个属性表的预订,架构是:

property_id, start_date, end_date

这有一个属性 ID 列表(在另一个表中引用)以及每个属性的每个预订的 start_date 和 end_date。

我要解决的是一个查询,它将显示在接下来的 28 天内可用于 7 天住宿的所有属性 (DISTINCT)。

我不知道如何开始构建这个查询。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    您需要进行交叉连接(将表与自身连接)并将一条记录的完成日期与同一属性的另一条记录的开始日期进行比较。

    帮助您入门的快速示例(缺少 28 天内的条件,并且不能保证在所有情况下都能正常工作):

    create table bookings(id int, start datetime, finish datetime);
    
    insert into bookings values (1, '2015-05-18','2015-05-23');
    insert into bookings values (1, '2015-05-25','2015-06-01');
    insert into bookings values (2, '2015-05-18','2015-05-29');
    insert into bookings values (2, '2015-06-15','2015-06-21');
    
    select distinct a.id  
    from bookings a cross join bookings b 
    where 
    a.id = b.id and a.start != b.start and datediff(a.start,b.finish) >= 7;
    

    【讨论】:

    • 我知道SELECT DISTINCT a.property_id FROM availability a CROSS JOIN availability b WHERE a.property_id = b.property_id AND a.start_date != b.start_date AND datediff(a.start_date,b.end_date) >= 7 AND a.start_date < NOW() + INTERVAL 28 DAY AND a.end_date > NOW() + INTERVAL 28 DAY; 正在返回结果......但不是正确的?!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2018-02-19
    • 1970-01-01
    • 2020-03-13
    • 2013-11-23
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多