MySQL预订系统:获取房间

时间:2015-06-13 17:22:53

标签: mysql

我的查询问题是在简单的酒店预订系统中查找可用房间。

我的表格结构如下所示:

hotels
hotelroomtypes(与酒店相交,有房间和房型)
bookings
orders

还有更多内容,但其他表的使用在查询中总结了自己,如下所示:

    SELECT DISTINCT
         hotels.HotelName
        ,hotels.HotelID
        ,hotels.Address
        ,hotels.Description
        ,images.URL
        ,roomtypes.Price
        ,roomtypes.RoomtypeName
        ,roomtypes.RoomtypeID
        ,COUNT(DISTINCT hotelroomtypes.HRID) AS AvailableRooms

    FROM hotelroomtypes
    INNER JOIN hotels ON (
        hotelroomtypes.HotelID = hotels.HotelID
        AND
        hotels.CountryID = 1 // e.g. United States
    )
    INNER JOIN roomtypes ON (
        hotelroomtypes.RoomtypeID = roomtypes.RoomtypeID
        AND
        roomtypes.RoomtypeID = 1 // e.g. Suite
    )
    RIGHT OUTER JOIN bookings ON (
        hotelroomtypes.HRID NOT IN (
            SELECT HRID FROM bookings
            WHERE bookings.From BETWEEN '2015-06-13' and '2015-06-16'
            OR bookings.To BETWEEN '2015-06-13' and '2015-06-16'
        )
    )
    INNER JOIN images ON (
        hotels.ImageID = images.ImageID
    )
    GROUP BY 
        hotels.HotelName
    HAVING COUNT(hotelroomtypes.HRID) > 0
    ORDER BY hotels.HotelName ASC, AvailableRooms ASC

现在我的问题在于获得正确数量的预订房间,如果所有房间都被占用,还会将酒店隐藏起来。

我过去通过在INNER JOIN上设置bookings来做到这一点,但当我有一个空的bookings表时,查询失败了100%并且没有显示任何酒店。

LEFT加入时,我偶尔会有一些误报,但目前使用这些类型的连接都会返回酒店的每个房间或者根本没有。

如果没有预订,

INNER自然会返回没有酒店。这是我之前使用的。

我如何正确地构建这个查询,以便为每个酒店提供确切数量的可用房间,以及只有可用房间的返回酒店,即使没有可以搜索的预订?

编辑表格结构:

Hotels:
 _________________________________________________________________
| hotelid | hotelname | countryid | description | otherfieldshere |
|_________|___________|___________|_____________|_________________|
|    1    |  example  |     1     |  something  |    something    |
|_________|___________|___________|_____________|_________________|

Hotelroomtypes:
 ______________________________________
| hrid | hotelid | roomtypeid | roomid |
|______|_________|____________|________|
|  1   |    1    |     1      |   1    |
|______|_________|____________|________|

Bookings:
 _______________________________________________
| bookingid |    from    |     to     | orderid |
|___________|____________|____________|_________|
|    1      | 2015-05-13 | 2015-05-16 |    1    |
|___________|____________|____________|_________|

Orders:

 ________________________________________
| orderid | reference |      email       |
|_________|___________|__________________|
|    1    |   12345   | some@example.com |
|_________|___________|__________________|

Rooms:

 _____________________
| roomid | roomnumber |
|________|____________|
|   1    |    500     |
|________|____________|

1 个答案:

答案 0 :(得分:0)

不幸的是,当你连续工作7小时而不休息时,酒精和解决问题的方法似乎并不是真正的选择。

我将查询重新构建为以下内容:

    SELECT DISTINCT
         hotels.HotelName
        ,hotels.HotelID
        ,hotels.Address
        ,hotels.Description
        ,images.URL
        ,roomtypes.Price
        ,roomtypes.RoomtypeName
        ,roomtypes.RoomtypeID
        ,COUNT(DISTINCT hotelroomtypes.HRID) AS AvailableRooms

    FROM hotels
    INNER JOIN hotelroomtypes ON (
        hotelroomtypes.HotelID = hotels.HotelID
        AND
        hotels.CountryID = ?
        AND
        hotelroomtypes.HRID NOT IN (
            SELECT HRID FROM bookings
            WHERE bookings.From BETWEEN ? and ?
            OR bookings.To BETWEEN ? and ?
        )
    )
    INNER JOIN roomtypes ON (
        hotelroomtypes.RoomtypeID = roomtypes.RoomtypeID
        AND
        roomtypes.RoomtypeID = ?
    )
    INNER JOIN images ON (
        hotels.ImageID = images.ImageID
    )
    GROUP BY 
        hotels.HotelName
    HAVING COUNT(hotelroomtypes.HRID) > 0
    ORDER BY hotels.HotelName ASC, AvailableRooms ASC

这实际上解决了我的问题。