【问题标题】:MySQL Case Room AvailabilityMySQL 案例室可用性
【发布时间】:2014-07-15 04:27:45
【问题描述】:

我有桌子:预订、预订、房间、住宿信息

样本数据: 好的首先抱歉加密示例数据给您带来不便

这是与 room_type 相关的表

   mysql> SELECT
        -> stay_info.room, room.code
        -> FROM stay_info
        -> RIGHT JOIN t_room
        -> ON stay_info.room = room.code
        -> INNER JOIN room_type
        -> ON room_type.code = room.roomType
        -> WHERE room_type.name = "Deluxe Double";




+--------------------------------------+--------------------------------------+
| room                                 | code                                 |
+--------------------------------------+--------------------------------------+
| NULL                                 | 26a73433-d0cc-4e93-95d9-453d362e85a7 |
| NULL                                 | 2b166d4f-2fe4-404c-beff-482c7d81c103 |
| NULL                                 | 3efc3bff-9c02-43ef-a494-e887a342c1f5 |
| NULL                                 | 7e0ebe37-a23a-46b6-9351-ba0c952ed33e |
| NULL                                 | 9574eb5f-58de-427f-859e-d8b61e289836 |
| NULL                                 | 9ee50e45-f92b-46bd-bf3e-fc818a96a81f |
| NULL                                 | d72f3f7e-c9d2-44d8-8767-66d2a1477a1b |
| NULL                                 | e25587a3-3dc1-4c0f-b4c2-68a1da538bd7 |
| NULL                                 | e2d7fe3a-06e2-48df-a083-6c8eaeeefc22 |
| NULL                                 | fadc4d40-33b2-4545-98fe-e52d351c50f9 |
+--------------------------------------+--------------------------------------+

10 行(0.00 秒)

如果我有预订流程

mysql> SELECT
    -> stay_info.room, IF(reservation.`status` = 1, room.code, (IF (stay
_info.room IS null , room.code,null))) as code
    -> FROM stay_info
    -> INNER JOIN reservation
    -> ON reservation.stayInfo = stay_info.code
    -> RIGHT JOIN room
    -> ON stay_info.room = room.code
    -> INNER JOIN room_type
    -> ON room_type.code = room.roomType
    -> WHERE room_type.name = "Deluxe Double" ;





+--------------------------------------+--------------------------------------+
| room                                 | code                                 |
+--------------------------------------+--------------------------------------+
| NULL                                 | 26a73433-d0cc-4e93-95d9-453d362e85a7 |
| NULL                                 | 2b166d4f-2fe4-404c-beff-482c7d81c103 |
| NULL                                 | 3efc3bff-9c02-43ef-a494-e887a342c1f5 |
| NULL                                 | 7e0ebe37-a23a-46b6-9351-ba0c952ed33e |
| NULL                                 | 9574eb5f-58de-427f-859e-d8b61e289836 |
| NULL                                 | 9ee50e45-f92b-46bd-bf3e-fc818a96a81f |
| NULL                                 | d72f3f7e-c9d2-44d8-8767-66d2a1477a1b |
| e25587a3-3dc1-4c0f-b4c2-68a1da538bd7 | NULL                                 |
| NULL                                 | e2d7fe3a-06e2-48df-a083-6c8eaeeefc22 |
| NULL                                 | fadc4d40-33b2-4545-98fe-e52d351c50f9 |
+--------------------------------------+--------------------------------------+

10 行(0.00 秒)

所以 room.code 字段是可用的房间。 reservation.status = 1 表示预订无效,所以它将是可用房间,因为激活时的状态预订为 0,因此 room.code 为 NULL

预订详情也是如此

   mysql> SELECT
        -> stay_info.room, IF(booking_details.`status` = 1, room.code, (IF (stay_info.room IS null , room.code,null))) as code
        -> FROM stay_info
        -> INNER JOIN booking_details
        -> ON booking_details.stayInfo = stay_info.code
        -> RIGHT JOIN room
        -> ON stay_info.room = room.code
        -> INNER JOIN room_type
        -> ON room_type.code = room.roomType
        -> WHERE room_type.name = "Deluxe Double" ;





+--------------------------------------+--------------------------------------+
| room                                 | code                                 |
+--------------------------------------+--------------------------------------+
| 26a73433-d0cc-4e93-95d9-453d362e85a7 | NULL                                 |
| NULL                                 | 2b166d4f-2fe4-404c-beff-482c7d81c103 |
| NULL                                 | 3efc3bff-9c02-43ef-a494-e887a342c1f5 |
| NULL                                 | 7e0ebe37-a23a-46b6-9351-ba0c952ed33e |
| NULL                                 | 9574eb5f-58de-427f-859e-d8b61e289836 |
| NULL                                 | 9ee50e45-f92b-46bd-bf3e-fc818a96a81f |
| NULL                                 | d72f3f7e-c9d2-44d8-8767-66d2a1477a1b |
| NULL                                 | e25587a3-3dc1-4c0f-b4c2-68a1da538bd7 |
| NULL                                 | e2d7fe3a-06e2-48df-a083-6c8eaeeefc22 |
| NULL                                 | fadc4d40-33b2-4545-98fe-e52d351c50f9 |
+--------------------------------------+--------------------------------------+

10 行(0.00 秒)

当我将上面的两个关系表与内部连接结合起来时

mysql> SELECT
-> stay_info.room,
-> IF((booking_details.`status` = 1 or reservation.`status` = 1), room.code, (IF (stay_info.room IS null , room.code,null))) as code
-> FROM stay_info
-> INNER JOIN booking_details
-> ON booking_details.stayInfo = stay_info.code
-> INNER JOIN reservation
-> ON reservation.stayInfo = stay_info.code
-> RIGHT JOIN room
-> ON stay_info.room = room.code
-> INNER JOIN room_type
-> ON room_type.code = room.roomType
-> WHERE room_type.name = "Deluxe Double" ;





+------+--------------------------------------+
| room | code                                 |
+------+--------------------------------------+
| NULL | 26a73433-d0cc-4e93-95d9-453d362e85a7 |
| NULL | 2b166d4f-2fe4-404c-beff-482c7d81c103 |
| NULL | 3efc3bff-9c02-43ef-a494-e887a342c1f5 |
| NULL | 7e0ebe37-a23a-46b6-9351-ba0c952ed33e |
| NULL | 9574eb5f-58de-427f-859e-d8b61e289836 |
| NULL | 9ee50e45-f92b-46bd-bf3e-fc818a96a81f |
| NULL | d72f3f7e-c9d2-44d8-8767-66d2a1477a1b |
| NULL | e25587a3-3dc1-4c0f-b4c2-68a1da538bd7 |
| NULL | e2d7fe3a-06e2-48df-a083-6c8eaeeefc22 |
| NULL | fadc4d40-33b2-4545-98fe-e52d351c50f9 |
+------+--------------------------------------+

10 行(0.00 秒)

我对组合的期望是

+--------------------------------------+--------------------------------------+
| room                                 | code                                 |
+--------------------------------------+--------------------------------------+
| 26a73433-d0cc-4e93-95d9-453d362e85a7 | NULL                                 |
| NULL                                 | 2b166d4f-2fe4-404c-beff-482c7d81c103 |
| NULL                                 | 3efc3bff-9c02-43ef-a494-e887a342c1f5 |
| NULL                                 | 7e0ebe37-a23a-46b6-9351-ba0c952ed33e |
| NULL                                 | 9574eb5f-58de-427f-859e-d8b61e289836 |
| NULL                                 | 9ee50e45-f92b-46bd-bf3e-fc818a96a81f |
| NULL                                 | d72f3f7e-c9d2-44d8-8767-66d2a1477a1b |
| e25587a3-3dc1-4c0f-b4c2-68a1da538bd7 | NULL                                 |
| NULL                                 | e2d7fe3a-06e2-48df-a083-6c8eaeeefc22 |
| NULL                                 | fadc4d40-33b2-4545-98fe-e52d351c50f9 |
+--------------------------------------+--------------------------------------+

所以我可以很容易地从代码字段中获得可用的房间

【问题讨论】:

  • 很难知道你需要什么。您可以发布您的表格和案例的架构吗?
  • 我们需要清晰的场景而不是表的数据。只能用文字解释吗?
  • 就像上面最后一张表一样,我希望我的组合是这样的,但不幸的是,我不知道要进行什么查询,我认为如果 INNER JOIN booking_details 和预订我会得到我的预期结果,但事实所有房间字段值都是 NULL,因此 room.code 的代码初始值是我预订或预订时的可用房间 room.code 为 NULL 并将 room.code 存储在 stay_info.room 的房间初始值与 booking_details.so 相同的行为来自代码(room.code)我知道可用的房间。
  • 您应该已经修改了现有问题。 (虽然我也觉得这个不透明)。您的结果数据仍然没有意义。 code 代表什么?如果分配了room,你为什么要null呢?仍然需要示例起始数据(即表中的基本行)。

标签: mysql sql


【解决方案1】:

我找到了我自己的问题的解决方案 这是解决方案

SELECT t_room_type.code, t_room_type.name,COUNT(t_room_type.name) AS numRoom FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code WHERE t_room.code 不在(SELECT DISTINCT t_stay_info.room FROM t_stay_info 左外连接 t_reservation ON t_stay_info.code = t_reservation.stayInfo 左外连接 t_booking_details ON t_stay_info.code = t_booking_details.stayInfo 左外连接 t_walk_in ON t_stay_info.code = t_walk_in.stayInfo 左外连接 t_stay_info_details ON t_stay_info.code = t_stay_info_details.stayInfo WHERE t_stay_info_details.stayDate 在“2014-07-16”和“2014-07-17”之间和(t_reservation.status = '0' 或 t_booking_details.status = '0' 或 t_walk_in.status = '0')) 分组 t_room_type.name 按 t_room_type.name ASC 排序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2012-06-15
    相关资源
    最近更新 更多