简体   繁体   中英

Get next available date from start date and end date

I want available dates from database table.

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

Here is I added table from database. When user select start date and end date than check in database. If already in between date of start date and end date then result want available date.

User start date = 2018-12-05

End date = 2018-12-20

Need result:

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

To really solve this problem, you need a Calendar table with one row per day.

With such a table, you can do:

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
                 );

I note that this does not take room_number into account at all -- because your question does not mention that at all. If you have a question that involves room_number , ask another question, with appropriate sample data, desired results, and explanation of what you want to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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