【问题标题】:Hotel Booking application rooms avaiability酒店预订申请房间可用性
【发布时间】:2016-02-23 04:46:28
【问题描述】:

我正在开发一个酒店预订应用程序,您可以在其中选择日期和房间类型,该应用程序会显示可用房间。我正在使用 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)

有什么想法吗?

【问题讨论】:

    标签: mysql sql spring spring-mvc


    【解决方案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 在您的情况下可能为空,如果确实可以,查询应该反映这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 2011-08-18
      • 1970-01-01
      • 2016-03-29
      • 2012-03-27
      • 2021-07-01
      • 2016-09-23
      相关资源
      最近更新 更多