如何在单个查询中获取酒店总房间和预订房间

时间:2018-12-04 06:59:07

标签: mysql

如何通过单个查询获取酒店的总房间和预订房间。

我用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 |
+------------------------------------------+------------+

3 个答案:

答案 0 :(得分:0)

希望我能正确理解您的问题。

请检查以下查询。

value

答案 1 :(得分:0)

使用依赖子查询。

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表中是唯一的。如果没有,则必须将distinct放在第一个SELECT后面。

答案 2 :(得分:-1)

您可以使用子查询 试试这个:

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;