mysql - 我想获取一个月内的一些行值作为列日明智

标签 mysql

我想获取一些行值作为读取日期基础的列的总和

SELECT * FROM `mitre_details`;

id       reading            date
1         500               2019-07-15
2         300               2019-07-15
3         600               2019-07-15
4         800               2019-07-15
5         900               2019-07-15
1         200               2019-07-16
2         250               2019-07-16
3         400               2019-07-16
4         550               2019-07-16
5         790               2019-07-16

尝试查询:

SELECT date,sum(reading) FROM `mitre_details` GROUP BY date;

期望的输出:

Date          Reading    Mitre 1    Mitre 2   Mitre 3   Mitre 4   Mitre 5
2019-07-15     3100       500         300       600       800       900
2019-07-16     2190       200         250       400       550       790

最佳答案

试试这个,

select `date`,
sum(reading) Reading,
group_concat(case when id=1 then reading end) m1,
group_concat(case when id=2 then reading end) m2,
group_concat(case when id=3 then reading end) m3,
group_concat(case when id=4 then reading end) m4,
group_concat(case when id=5 then reading end) m5
from mitre_details group by `date`;

关于mysql - 我想获取一个月内的一些行值作为列日明智,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054599/

相关文章:

php - 警告 : mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql. socks )中

php - 带有条件的sql插入查询 - 不存在

java - 使用多个查询从不同表中获取数据

php - SQL 更新查询无法正确处理 php 数据

mysql - 如何在 MySQL 中随机选择 HEX 颜色但避免特定颜色范围?

php - 在codeigniter中将变量从View页面传输到Controller页面是否理想?

mysql - 使用 Perl 查找复合主键的一部分

c# - 文件上传时覆盖

php - 将多个产品添加到一个购物车中

java - ANDROID:mysql JDBC 中的用户登录认证