2015-05-18 27 views
0

我有一個這樣的表。現在我想要兩個總和(小時).. 1爲place_type'1',另一個爲place_type'2'。在MySQL中可能嗎?現在在mysql中使用值的總和

我的查詢是

SELECT sum(hour) as totalHour from category_data group by category_data.user_id 

也試圖與IF條件,但不是事先工作

SELECT IF(place_type='1',sum(hour),0) home, 
    IF(place_type='2', sum(hour), 0) center, 
    from category_data group by category_data.user_id 

user_id place_type hour  
    1   1   2 
    1   2   5 
    2   1   3 
    2   2   4 
    3   1   2 

感謝

+0

我有這樣 – Strawberry

回答

3

條件聚合您這裏需要。

select sum(case when place_type = 1 then `hour` end), 
     sum(case when place_type = 2 then `hour` end) 
    from category_data 

Example fiddle

+0

感謝的答案很多....做工精細 –

+0

@SRana沒問題 - 隨時接受它時,它可以讓你 –