MySql 查询以查找给定日期“从”和“到”可用的房间

Posted

技术标签:

【中文标题】MySql 查询以查找给定日期“从”和“到”可用的房间【英文标题】:MySql query to find rooms available given dates 'From' and 'to' 【发布时间】:2018-11-15 06:46:13 【问题描述】:

表格如下

rooms(RoomId(PK)(int),
      RoomName(varchar))

bookings(RoomId(FK)(int),
         From_D(date),
         To_D(date),
         Bookee(varchar))

(PK-primary Key ,FK-foreign Key)

鉴于arrival(From_D)departure(To_D) 日期,我如何找出可用房间的ID

我试过了:

SELECT DISTINCT RoomId 
FROM bookings 
WHERE  arrival not in(From_D,To_d) AND 
       departure not in(From_D,To_d)

但是,如果在数据库中存储了多个房间预订,并且即使该房间在该日期范围内不是免费的,它也会预订房间

【问题讨论】:

请以表格格式提供示例数据和您的预期输出 【参考方案1】:

考虑下图(笔迹不好见谅):

在上图中,我们可以在横轴上看到日期范围。 From_DTo_D 表示特定房间的预订空档。到达和离开日期考虑了四种情况(用 Ai 和 Di 表示)。

我们可以清楚地看到,只有当到达和离开日期完全在预订时段的左侧或右侧时,特定房间才可用

我们可以在RoomIDGROUP BY 并只考虑那些房间,其中每次预订都遵循上面定义的标准,使用HAVING 子句。

查询将如下所示:

SELECT RoomID 
FROM bookings 
GROUP BY RoomID 
HAVING 
  SUM((From_D < :arrival AND To_D < :arrival) 
             OR 
      (From_D > :departure AND To_D > :departure)) = COUNT(*)

Demo on DB Fiddle

CREATE TABLE `rooms` (
 `RoomId` int(4) NOT NULL,
 `RoomName` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `bookings` (
 `RoomId` int(4) NOT NULL,
 `From_D` date NOT NULL,
 `To_d` date NOT NULL,
 `B_Name` varchar(20) NOT NULL,
 KEY `RoomId` (`RoomId`),
 CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`RoomId`) REFERENCES `rooms` (`RoomId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `rooms` (`RoomId`, `RoomName`) VALUES 
('1', 'Auditorium'), 
('2', 'Room2'), 
('3', 'Room3'), 
('4', 'Room4'), 
('5', 'Room5');

INSERT INTO `bookings` (`RoomId`, `From_D`, `To_d`, `B_Name`) VALUES 
('1', '2018-11-01', '2018-11-03', 'Trance'), 
('2', '2018-11-02', '2018-11-07', 'Alcoding'), 
('3', '2018-11-01', '2018-11-04', 'DebSoc'), 
('4', '2018-11-12', '2018-11-17', 'MunSoc'), 
('5', '2018-11-03', '2018-11-06', 'Pulse');

查询:检查 2018-11-01 和 2018-11-03 之间的任何可用性

SELECT RoomId 
FROM bookings 
GROUP BY RoomID 
HAVING 
  SUM((From_D < '2018-11-01' AND To_D < '2018-11-01') 
             OR 
      (From_D > '2018-11-03' AND To_D > '2018-11-03')) = COUNT(*)

结果:根据示例数据,只有 RoomId 4 可用

| RoomId |
| ------ |
| 4      |

【讨论】:

我尝试运行此查询,但对于某些房间空闲的日期,它返回零行 @TanmayShrivastava 能否请您提供该查询失败的表中示例数据的 SQL SELECT DISTINCT b1.RoomId FROM bookings AS b1 WHERE NOT EXISTS ( SELECT 1 FROM bookings AS b2 WHERE b2.RoomId = b1.RoomId AND NOT('2018-11-01'&lt; b2.From_D AND '2018-11-03' &lt; b2.From_D) OR NOT('2018-11-01' &gt; b2.To_D AND '2018-11-03' &gt; b2.To_D) ) 我知道在给定日期之间有一个房间是空闲的,但查询返回零结果 完成,我已经设置好了数据【参考方案2】:

在您的日期中使用 BETWEENAND

SELECT DISTINCT RoomId FROM bookings    
WHERE (date_field BETWEEN From_D AND To_d)

【讨论】:

【参考方案3】:
select distinct r.*
from rooms r
    left join bookings b on b.RoomId=r.RoomId
where b.RoomId is null or @arrival > To_D or @departure < from_td

【讨论】:

只有代码的答案很差,你能解释一下你的代码是做什么的吗? @arrival 应该怎么做?这看起来不像是有效的 SQL【参考方案4】:
Try this :
    SELECT * FROM   rooms room1
    WHERE  NOT EXISTS (SELECT *
                FROM   booking booking1
                       WHERE  room1.roomid = booking1.roomid AND
                              (:from_d BETWEEN booking1.from_d AND booking1.to_d OR
                               :to_d   BETWEEN booking1.from_d AND booking1.to_d))

【讨论】:

以上是关于MySql 查询以查找给定日期“从”和“到”可用的房间的主要内容,如果未能解决你的问题,请参考以下文章

查找预订重叠以检查日期可用性

Sql查询以查找两个给定日期之间的视图百分比差异

SQL 查询以查找给定日期之前的最后一次更改

MySQL查询根据日期查找最长的值序列

Mysql Select Query 以查找在两个给定日期之间也部分空闲的项目

我如何查询给定日期范围的mysql并加入两个表?