我有一张这样的 table 。现在我想要两个总和(小时).. 1 用于 place_type '1',另一个用于 place_type '2'。在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
提前致谢
最佳答案
这里需要条件聚合。
select sum(case when place_type = 1 then `hour` end),
sum(case when place_type = 2 then `hour` end)
from category_data
关于php - mysql中使用条件的值总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30296618/