簡體   English   中英

MySQL案例室可用性

[英]MySQL Case Room Availability

我有桌子:預訂,預訂,房間,stayed_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表示預訂無效,因此它將是可用的房間,因為處於活動狀態的status預訂為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 |
+--------------------------------------+--------------------------------------+

這樣我就可以從代碼字段輕松獲得可用空間

我已經找到了我自己的問題的解決方案,這就是解決方案

選擇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在哪里t_room.code NOT IN(選擇DISTINCT t_stay_info.room從t_stay_info ON左輸出.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“之間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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM