【问题标题】:Get next available date from start date and end date从开始日期和结束日期获取下一个可用日期
【发布时间】:2018-12-02 12:00:04
【问题描述】:

我想要数据库表中的可用日期。

Room number Start_date  End_date
room 1  2018-12-01  2018-12-05
room 1  2018-12-08  2018-12-15
room 1  2018-12-20  2018-12-31

这是我从数据库中添加的表。 当用户选择开始日期和结束日期而不是签入数据库时​​。如果已经在开始日期和结束日期之间,则结果需要可用日期。

用户开始日期 = 2018-12-05

结束日期 = 2018-12-20

需要结果:

2018-12-06
2018-12-07
2018-12-16
2018-12-17
2018-12-18
2018-12-19

【问题讨论】:

  • 你试图解决什么问题?
  • 我想在用户为房间 1 选择的日期之间获取可用日期

标签: mysql sql


【解决方案1】:

要真正解决这个问题,您需要一个每天一行的Calendar 表。

有了这样的表,你可以做到:

select c.date
from calendar c
where c.date >= '2018-12-05' and
      c.date <= '2018-12-20' and
      not exists (select 1
                  from t
                  where c.date >= start_date and
                        c.date <= end_date
                 );

我注意到这根本没有考虑room_number——因为你的问题根本没有提到这一点。如果您的问题涉及room_number,请提出另一个问题,并提供适当的样本数据、期望的结果以及您想要完成的任务的说明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多