我试图用MySql数据库中的数据填充Morris图表的数据数组。

我有这样的表“ buchungen”:

ID   Value   Kind   Date
1    200     L      2016-01-01
2    250     B      2016-01-01
3    250     L      2016-01-01
4    200     B      2016-02-01
5    250     B      2016-02-01
6    200     L      2016-02-01


我想做一个查询,给我这样的输出:

价值总和,按数据和种类分组。

一月份:450,L和250 B

2月:450 B和200 L

所需的结构:

[{"m":"1","a":"450","b":"250},{"m":"2","a":"200","b":"450}]


用我的查询:

SELECT MONTH( date ) AS m, SUM( value ) AS a
FROM  `buchungen`
WHERE YEAR( date ) = YEAR( CURDATE( ) )
GROUP BY MONTH( date )


我得到以下结构:

[{"m":"4","a":"476852.64"},{"m":"5","a":"851866.74"}]


我有一个我不知道如何考虑“种类”领域的问题。我怎样才能做到这一点?

谢谢!

最佳答案

尝试这个:

SELECT MONTH( date ) AS m,
       SUM(CASE WHEN Kind = 'L' THEN value ELSE 0 END) AS a,
       SUM(CASE WHEN Kind = 'B' THEN value ELSE 0 END) AS b
FROM  `buchungen`
WHERE YEAR( date ) = YEAR( CURDATE( ) )
GROUP BY MONTH( date )

关于php - 选择总和并按种类分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37386286/

10-11 06:07