mysql - 酒店预订申请 客房供应情况

标签 mysql sql spring spring-mvc

我正在开发一款酒店预订应用程序,您可以在其中选择日期和房间类型,应用程序会显示可用的房间。我正在使用 Java Spring 框架来执行此操作。

这是我认为对该查询重要的表:

CREATE TABLE IF NOT EXISTS `booking` (
  `id` bigint(20) NOT NULL,
  `aproved` bit(1) NOT NULL,
  `begin_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `room_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `room` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `room_type_id` bigint(20) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=161 DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `room_type` (
  `id` bigint(20) NOT NULL,
  `number_of_rooms` int(11) NOT NULL,
  `price` int(11) NOT NULL,
  `type` varchar(255) DEFAULT NULL,
  `hotel_id` bigint(20) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;

我在进行该查询时遇到困难...

我做了这个,但加入预订房间并不是一个好主意,因为这只会在有预订时选择房间...

SELECT * 
FROM room r join booking b on b.room_id = r.id join room_type rt on r.room_type_id = rt.id 
WHERE not ((b.begin_date >= :initDate And b.begin_date <= :endDate) or (b.begin_date >= :initDate And b.end_date <= :endDate) or (b.begin_date <= :initDate and b.end_date >= :endDate) and b.aproved = true and rt.id = :roomType)

有什么想法吗?

最佳答案

select * from rooms r
where r.room_type_id = :desiredRoomType
and not exists (
 select * from bookings b
 where begin_date >= :desiredDate
 and end_date <= :desiredDate
)

我不确定,为什么在你的情况下 begin_date/end_date 可能为空,如果它们确实可以,查询应该反射(reflect)这一点。

关于mysql - 酒店预订申请 客房供应情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831055/

相关文章:

php - 从目录中获取文件并将内容插入 SQL

java - 类型不匹配 : cannot convert from List<Map<String, Object>> 到 List<Object>

带有 spring.config.location 的 Spring

Mysql 查询不适用于 WHERE BETWEEN

PHP MySQLi 转义语句中的转义参数可能存在问题?

mysql - 连接超时后重新连接到mysql

php - 在 Codeigniter 中从 Excel 导入时,无法使用 where 子句选择数据

sql - Postgres : integer out of range

sql - Oracle Connect BY 仅返回顶层

java - 我无法通过 javamelody 监控 spring 应用程序