Mysql 日期范围之间的Sql搜索日期
我在数据库中有两个表 房间:包含房间的详细信息 预订:包含关于房间的预订信息 房间:Mysql 日期范围之间的Sql搜索日期,mysql,sql,date-range,Mysql,Sql,Date Range,我在数据库中有两个表 房间:包含房间的详细信息 预订:包含关于房间的预订信息 房间: +----+----------+---------------+---------+ | id | hotel_id | room_category | room_no | +----+----------+---------------+---------+ | 1 | 1 | delux | 1 | | 2 | 1 | delux
+----+----------+---------------+---------+
| id | hotel_id | room_category | room_no |
+----+----------+---------------+---------+
| 1 | 1 | delux | 1 |
| 2 | 1 | delux | 2 |
| 3 | 1 | delux | 3 |
| 4 | 1 | delux | 4 |
| 5 | 1 | delux | 5 |
+----+----------+---------------+---------+
预订:
+----+------------+----------+---------------+--------------+---------------+---------+----------------+
| id | booking_id | hotel_id | room_category | checkin_date | checkout_date | room_no | booking_status |
+----+------------+----------+---------------+--------------+---------------+---------+----------------+
| 1 | 1 | 1 | delux | 2016-08-25 | 2016-08-30 | 1 | y |
| 2 | 2 | 1 | delux | 2016-08-25 | 2016-08-28 | 2 | y |
+----+------------+----------+---------------+--------------+---------------+---------+----------------+
现在我写一个查询:
select * from rooms
where
hotel_id =1 and
room_category="delux"
and id not in(
select room_no
from bookings
where
checkin_date between '2016-08-25' and '2016-08-28'
or
checkout_date between '2016-08-25' and '2016-08-28'
);
问题:
1.当搜索26-27的可用性时,结果也包含1号和2号房间检查重叠时段的通常逻辑是:begin_1
select * from rooms
where hotel_id =1
and room_category="delux"
and id not in
(
select room_no
from bookings
where checkin_date < '2016-08-28'
and checkout_date >= '2016-08-25'
);
到底是什么问题?对于26/27,内部查询将不返回任何内容,并且您正在使用not in,因此应返回所有内容问题不清楚@Belal他的叙述不是很好,但问题很清楚,如果他搜索26号到27号,而不是他显示的25号和28号,那么签入日期不在26号和27号之间,签出日期也不在26号和27号之间。他没有正确地组织他的未婚妻关系,也没有说明何时签入日期<26日,何时签出日期>27th@dhyanandra,请出示主键和外键?@Belal他们似乎很明显是酒店id,房间号是关系