mysql - mysql中具有列类别的行的总和

标签 mysql sum

我在 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/

相关文章:

python - 从 MySQL 表到 Python 列表

mysql - 按位掩码中设置的位数排序

sql - 使用sqlite查询从amout值中删除dot(.)

R 按索引(日期)对多个数据帧的列表求和

python - 特定行 Pandas 数据框的总和

python - 符号条件和

php - 从存储过程 MySQL 返回变量

php - MySQL 将行更改为列

php - undefined variable : delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1. php 第 61 行

MySQL 兼容性或相似度排名查询