php - mysql 按日期的数据透视表

标签 php mysql

我正在尝试显示包含按天特定列总和的付款表的结果;

payment id | payment_actual_timestamp | payment type | amount
1          | 2015-11-23 13:01:20      | AUTH         | 0.16
2          | 2015-11-23 13:07:20      | AUTH         | 23.65
3          | 2015-11-24 08:07:20      | VOID         | 19.24
4          | 2015-11-24 13:45:20      | AUTH         | 0.65
5          | 2015-11-24 16:34:10      | REFUND       | 1.20
6          | 2015-11-25 13:07:20      | SETTLE       | 4.40

我想显示的内容如下;

Date       | SETTLE | AUTH | VOID | REFUND
2015-11-23 | 0.00   | 23.81| 0.00 | 0.00
2015-11-24 | 0.00   | 0.65 | 19.24| 1.20
2015-11-25 | 4.40   | 0.00 | 0.00 | 0.00

有办法做到这一点吗?

非常感谢。

最佳答案

一种选择是将条件聚合sumcase结合使用:

select date(payment_actual_timestamp), 
   sum(case when payment_type = 'SETTLE' then amount end) settleamt,
   sum(case when payment_type = 'AUTH' then amount end) authamt,
   sum(case when payment_type = 'VOID' then amount end) voidamt,
   sum(case when payment_type = 'REFUND' then amount end) refundamt
from yourtable
group by date(payment_actual_timestamp)

关于php - mysql 按日期的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927985/

相关文章:

php - PHP 中的事件监听器

mysql - 无法在 Linux 中启动 MySQL

mysql - 触发插入到单独的表中

mysql - 使用Prepared语句对多条记录进行单个INSERT查询

php - php注释中额外星号的目的是什么?

PHP:如何获取特定VLAN的IP地址

php - 如何使用 whereBetween() 从选定的 DB::raw() 值进行查询

mysql - 在 where 子句 mysql 中完全匹配 utf8 中的单词

php - 从数据库显示图像

php - 查找超过 100K 个位置之间的距离