我在 mysql 中有如下表
表格数据:
id items color date
1 22 blue 2014-05-25
2 344 yellow 2014-05-26
3 255 blue 2014-05-26
4 234 blue 2014-05-26
5 200 yellow 2014-05-27
6 100 blue 2014-05-27
7 100 yellow 2014-05-25
8 233 blue 2014-05-26
我需要基于颜色的项目总和,并按日期分组...
喜欢
我需要这样的输出:
Date Items blue yellow
2014-05-25 122 22 100
2014-05-26 1066 722 344
2014-05-27 300 100 200
我需要 Mysql 查询来获取上面给出的输出..
最佳答案
您将按日期进行正常分组 并根据http://dev.mysql.com/doc/refman/5.0/en/case.html 你应该
select Date, sum(items) as Items,
sum(case when color = 'blue' then color else 0 end case) as blue,
sum(case when color = 'yellow' then color else 0 end case) as yellow
from ...
group by Date
抱歉,如果语法不匹配。我不做MySQL。
关于mysql - mysql中具有列类别的行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23883998/