MySql多重平均值

时间:2014-04-28 18:27:33

标签: mysql

我有一张桌子,其中我有不同类型的酒店房间的价格。 我想查询,以便我得到每个房间的平均价格,但是mySQL只给我1平方,而不是我需要的2。我的代码如下:

SELECT AVG(Price) From `Room` WHERE (Type='Double') OR (Type='single');

我想我的OR声明似乎让我感到烦恼..

2 个答案:

答案 0 :(得分:1)

SELECT AVG(Price), `Type`
FROM `Room`
GROUP BY `Type`

答案 1 :(得分:0)

SELECT AVG(case when Type='Double' then Price else 0 end) as avg_double_price,
       AVG(case when Type='Single' then Price else 0 end) as avg_single_price,
       sum(Type='Double') as double_count,
       sum(Type='Single') as single_count
From `Room`