Mysql查询 - 问题

时间:2017-04-12 12:01:10

标签: mysql

我遇到MySql查询的1个问题:

select sum(h.total_rooms) as total_rooms from reservation_details as rd LEFT JOIN hotels as h ON rd.hotel_id = h.id left join `chains` as `c` on `c`.`id` = `h`.`chain_id` where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5 and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id

以上查询返回:

total_rooms
48216
7700
13250

但是 我需要查询的总和。

1 个答案:

答案 0 :(得分:0)

尝试使用这样的sum()函数,并在查询中使用ROLLUP

select sum(h.total_rooms) as total_rooms from reservation_details as rd 
LEFT JOIN hotels as h ON rd.hotel_id = h.id 
left join `chains` as `c` on `c`.`id` = `h`.`chain_id` 
where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5 
and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id 
WITH ROLLUP