酒店预订申请房间可用性

时间:2015-11-20 16:04:20

标签: 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)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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可能在您的情况下为null,如果它们确实可以,则查询应该反映出来。