开发者

Help writing a mysql query

I'm getting a headache from this query, I have no idea how to solve it. Perhaps I should just do the math in PHP but I can't seem to do that either.. It's BRAINBREAKING.

I need data from combining two tables:

event_hotel_reservation:

Help writing a mysql query

an开发者_如何学运维d hotel_room_type:

Help writing a mysql query

I want to write a query which shows me the total beds per day per hotel. The total beds can be found by doing totalGuests * capacity.

I then want to order them by day, so I know how many beds I need to book per day, per hotel.

This is the query I tried, but failed miserably:

select ehr.day, (ehr.totalguests * hrt.capacity) as total 
from event_hotel_reservation ehr 
inner join hotel_room_type hrt on ehr.roomtypeid = hrt.roomtypeid 
group by day

Any help? thanks in advance.


You are close, but you have to also group by hotel. Try:

    SELECT ehr.day, (ehr.totalguests * hrt.capacity) AS total 
      FROM event_hotel_reservation ehr 
INNER JOIN hotel_room_type hrt 
        ON ehr.roomtypeid = hrt.roomtypeid 
  GROUP BY ehr.hotelID, ehr.day


I hope I interpreted your schema correctly

select ehr.hotelid, ehr.day, 
sum(hrt.capacity) as total_capacity, 
sum(ehr.totalguests) as total_used_capacity, 
sum(hrt.capacity - ehr.totalguests) as total_free_capacity
from event_hotel_reservation ehr 
inner join hotel_room_type hrt on ehr.roomtypeid = hrt.roomtypeid 
group by ehr.hotelid, ehr.day
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜