MySQL Group在Select上选择了行

时间:2012-11-05 17:55:29

标签: mysql

假设我有下表:

Room | Seats | Occupied
-----------------------
a1      20       10
b2      15       15
c3      45       30
d4      20       15
e5      10       10

我遇到了麻烦(根本不知道怎么做)生成一个SELECT语句,它会合并一些房间并总结它们的值并返回以下内容:

Room | Seats | Occupied
-----------------------
a1      20       10
b2c3d4  80       60
e5      10       10

如何做到这一点?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

select room,
       sum(seats) as seats,
       sum(occupied) as occupied
from your_table
group by case when room in ('b2', 'c3', 'd4') 
              then 'b2c3d4'
              else room
         end