簡體   English   中英

如何在單個查詢中獲取酒店總房間和預訂房間

[英]How to get hotel total room and booked room in single query

如何在單個查詢中獲得酒店的總房間和預訂房間。

我用2查詢得到結果。 我需要在單個查詢中。

由於所有計數都在此表中,因此我遇到的問題htl_room_information.id_hotel

預訂房間:

 SELECT x.hotel_name
      , count(i.id_hotel) room 
   FROM htl_booking_detail d
   JOIN htl_branch_info_lang x
     ON x.id=d.id_hotel 
   JOIN htl_room_information i 
     ON d.id_room=i.id 
  group 
     by x.hotel_name;

+------------------------------------------+------+
| hotel_name                               | room |
+------------------------------------------+------+
| hotel                                    |    3 |
| hotel1                                   |    1 |
| hotel2                                   |    4 |
| hotel3                                   |   13 |
| hotel4                                   |    9 |
| hotel5                                   |    3 |
| hotel6                                   |    3 |
| hotel7                                   |    2 |
+------------------------------------------+------+

房間總數

SELECT (htl_branch_info_lang.hotel_name) as hotel_name,count(htl_room_information.id_hotel) as total_room FROM htl_room_information ,htl_branch_info_lang where htl_room_information.id_hotel=htl_branch_info_lang.id group by htl_branch_info_lang.hotel_name;

+------------------------------------------+------------+
| hotel_name                               | total_room |
+------------------------------------------+------------+
| hotel                                    |        219 |
| hotel2                                   |         25 |
| hotel3                                   |         16 |
| hotel4                                   |          5 |
| hotel5                                   |         55 |
| hotel6                                   |         27 |
| hotel7                                   |         56 |
| hotel8                                   |         52 |
+------------------------------------------+------------+

希望我能正確理解您的問題。

請檢查以下查詢。

select b.hotel_name hotel_name, a.room , 
b.total_room
from
(SELECT htl_branch_info_lang.hotel_name, 
count(htl_room_information.id_hotel) as room FROM htl_booking_detail, 
htl_branch_info_lang, htl_room_information WHERE 
htl_branch_info_lang.id=htl_booking_detail.id_hotel and  
htl_booking_detail.id_room=htl_room_information.id group by 
htl_branch_info_lang.hotel_name ) a RIGHT join (SELECT (htl_branch_info_lang.hotel_name) as 
hotel_name,count(htl_room_information.id_hotel) as total_room 
FROM htl_room_information ,htl_branch_info_lang 
where htl_room_information.id_hotel=htl_branch_info_lang.id 
group by htl_branch_info_lang.hotel_name) b
on a.hotel_name = b.hotel_name
;

使用依賴的子查詢。

SELECT x.hotel_name,
    (
      SELECT count(i.id_hotel)  
      FROM htl_booking_detail d
      JOIN htl_room_information i 
         ON d.id_room=i.id 
      WHERE x.id=d.id_hotel 
    ) as room,
    (
      SELECT count(i.id_hotel) 
      FROM htl_room_information i
      WHERE i.id_hotel=x.id 
    ) as total_room 
FROM htl_branch_info_lang x

我假設hotel_namehtl_branch_info_lang表中是唯一的。 如果不是這樣,則必須在第一個SELECT后面放一個distinct的內容。

您可以使用子查詢試試看:

SELECT (a.hotel_name) as 
hotel_name,count(htl_room_information.id_hotel) as total_room, (SELECT 
count(htl_room_information.id_hotel) as room FROM htl_booking_detail, 
htl_branch_info_lang b, htl_room_information  WHERE 
b.id=htl_booking_detail.id_hotel and  
htl_booking_detail.id_room=htl_room_information.id and 
b.hotel_name = a.hotel_name), count(htl_room_information.id_hotel) - (SELECT 
count(htl_room_information.id_hotel) as room FROM htl_booking_detail, 
htl_branch_info_lang b, htl_room_information  WHERE 
b.id=htl_booking_detail.id_hotel and  
htl_booking_detail.id_room=htl_room_information.id and 
b.hotel_name = a.hotel_name) as available  FROM htl_room_information 
,htl_branch_info_lang 
a where htl_room_information.id_hotel=a.id group by 
a.hotel_name;

暫無
暫無

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

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