帮助编写mysql查询

时间:2011-08-31 15:35:58

标签: mysql

我对此查询感到头疼,我不知道如何解决它。也许我应该在PHP中进行数学计算,但我似乎也无法做到这一点......这是大脑突破。

我需要组合两个表的数据:

event_hotel_reservation:

event_hotel_reservation

和hotel_room_type:

hotel_room_type

我想写一个查询,向我显示每间酒店每天的总床位数。通过totalGuests * capacity可以找到总床位数。

然后我想在白天订购它们,所以我知道每个酒店每天需要预订多少张床。

这是我试过的查询,但失败了:

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

有任何帮助吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

你很近,但你必须按酒店分组。尝试:

    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

答案 1 :(得分:0)

我希望我正确解释您的架构

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
相关问题