简体   繁体   中英

How do i subtract from multiplier row , from 2 table? mySQL

I have been trying for hours search around for a way to subtract from multiplier row , from 2 table? As I keep getting error from mySQL workbench.

I have 2 table hotel , hotel_stay

I need to get the number of unique room then minus the individual capacity

+--------------------+------------------+
hotel_stay
+--------------------+------------------+
room (PK)
99
99
10
10
10

+--------------------+------------------+
hotel  
+--------------------+------------------+ 
room(FK)  |   capacity
99        |   10
10        |   12

Output will be on the same table as the hotel or I will have to us AS?

room | capacity
99   | 8      
10   | 9   

I am not sure I fully understand your question. But you can us something like this to get the total capacity for each room:

select h.room, 
   (h.capacity - hs.TotalBooked) as Capacity
from hotel h
inner join 
(
    select COUNT(room) as TotalBooked, 
       Room
    from hotel_stay
    group by room
) hs
    on h.room = hs.room

See SQL Fiddle with Demo . This returns the result:

| ROOM | CAPACITY |
-------------------
|   10 |        9 |
|   99 |        8 |

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