简体   繁体   中英

How to get hotel total room and booked room in single query

How to get hotel total room and booked room in single query.

I used 2 queries get result. I need this in single query.

The issue I am experiencing since all counts are in this table htl_room_information.id_hotel

Booked room:

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

Total Rooms

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

Hoping, I understood your question correctly.

Please check below query.

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
;

Use dependent subqueries.

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

I assume that the hotel_name is unique in the htl_branch_info_lang table. If not, you have to put distinct behind the first SELECT .

You can use subquery try this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM