MySQL案例室可用性

时间:2014-07-15 04:27:45

标签: mysql sql

我有桌子:预订,预订,房间,stay_information

样本数据: 好的,首先很抱歉加密样本数据带来的不便

这是与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字段是可用空间。预订。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 |
+--------------------------------------+--------------------------------------+

所以我将从代码字段轻松获得可用空间

1 个答案:

答案 0 :(得分:0)

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

  

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 NOT IN(SELECT DISTINCT   t_stay_info.room FROM t_stay_info LEFT OUTER JOIN t_reservation ON   t_stay_info.code = t_reservation.stayInfo LEFT OUTER JOIN   t_booking_details ON t_stay_info.code = t_booking_details.stayInfo   LEFT OUTER JOIN t_walk_in ON t_stay_info.code = t_walk_in.stayInfo   LEFT OUTER JOIN t_stay_info_details ON t_stay_info.code =   t_stay_info_details.stayInfo WHERE t_stay_info_details.stayDate   BETWEEN' 2014-07-16' AND' 2014-07-17' AND(t_reservation.status =' 0'   或者t_booking_details.status =' 0'或者t_walk_in.status =' 0'))GROUP BY   t_room_type.name ORDER BY t_room_type.name ASC