我有这样的表,
Mst_Booking
room_id int(11),
order_id int(11),
start datettime,
end datetime
我可以获得此查询预订的具体日期
select * from Mst_Booking etc..,
结果
1 1 2017-01-01 2017-01-08
1 1 2017-01-11 2017-01-12
但是我无法获得那里没有列出的数据 像这样
“未预订/可用日期”
room_id start end
1 2017-01-09 2017-01-10
1 2017-01-13 2017-01-31
请帮助我,如果我创建更多表来标记未预订?或者有查询按照我的要求检索数据?
答案 0 :(得分:0)
如果视图不打扰您,我们可以列出关于每个room_id的“未预订”日期,另一个>>
你的桌子:
mysql> select * from mst_booking;
+---------+----------+---------------------+---------------------+
| room_id | order_id | start_date | end_date |
+---------+----------+---------------------+---------------------+
| 1 | 1 | 2017-01-01 00:00:00 | 2017-01-08 00:00:00 |
| 1 | 2 | 2017-01-11 00:00:00 | 2017-01-12 00:00:00 |
| 1 | 3 | 2017-01-17 00:00:00 | 2017-01-21 00:00:00 |
| 1 | 4 | 2017-01-25 00:00:00 | 2017-01-27 00:00:00 |
| 2 | 5 | 2017-01-17 00:00:00 | 2017-01-21 00:00:00 |
| 2 | 6 | 2017-01-25 00:00:00 | 2017-01-27 00:00:00 |
| 3 | 7 | 2017-01-16 00:00:00 | 2017-01-19 00:00:00 |
| 3 | 8 | 2017-01-25 00:00:00 | 2017-01-29 00:00:00 |
+---------+----------+---------------------+---------------------+
列出“未预订”日期:
select room_id, adddate(end_date,1) as date from mst_booking where
room_id=1 UNION select room_id,subdate(start_date,1) as date from
mst_booking where start_date not in (select min(start_date) from
mst_booking where room_id=1) and room_id=1 order by date;
结果:
+---------+---------------------+
| room_id | date |
+---------+---------------------+
| 1 | 2017-01-09 00:00:00 |
| 1 | 2017-01-10 00:00:00 |
| 1 | 2017-01-13 00:00:00 |
| 1 | 2017-01-16 00:00:00 |
| 1 | 2017-01-22 00:00:00 |
| 1 | 2017-01-24 00:00:00 |
| 1 | 2017-01-28 00:00:00 |
+---------+---------------------+
您可以看到“未预订/可用”的日期范围列在另一个下方。
对于room_id = 2:
select room_id, adddate(end_date,1) as date from mst_booking where
room_id=2 UNION select room_id,subdate(start_date,1) as date from
mst_booking where start_date not in (select min(start_date) from
mst_booking where room_id=2) and room_id=2 order by date;
结果:
+---------+---------------------+
| room_id | date |
+---------+---------------------+
| 2 | 2017-01-22 00:00:00 |
| 2 | 2017-01-24 00:00:00 |
| 2 | 2017-01-28 00:00:00 |
+---------+---------------------+
对于room_id = 3:
select room_id, adddate(end_date,1) as date from mst_booking where
room_id=3 UNION select room_id,subdate(start_date,1) as date from
mst_booking where start_date not in (select min(start_date) from
mst_booking where room_id=3) and room_id=3 order by date;
结果:
+---------+---------------------+
| room_id | date |
+---------+---------------------+
| 3 | 2017-01-20 00:00:00 |
| 3 | 2017-01-24 00:00:00 |
| 3 | 2017-01-30 00:00:00 |
+---------+---------------------+