php - 选择名称相似的行

标签 php mysql select

所以我有一个像这样的 tbl_total。

Name   | Total  |  Month
=========================
David  | 87     | Jan
Mike   | 67     | Jan
David  | 90     | Feb
Mike   | 100    | Feb

我想这样显示。请有人告诉我该怎么做,因为我完全不知道如何在 PHP 中显示这样的内容。

Name   | Jan | Feb
===================
David  | 87  | 90
Mike   | 67  | 100

最佳答案

select name,
       sum(case when month = 'Jan' then total else 0 end) as Jan,
       sum(case when month = 'Feb' then total else 0 end) as Feb,
       sum(case when month = 'Mar' then total else 0 end) as Mar,
       sum(case when month = 'Apr' then total else 0 end) as Apr,
       sum(case when month = 'May' then total else 0 end) as May,
       sum(case when month = 'Jun' then total else 0 end) as Jun,
       sum(case when month = 'Jul' then total else 0 end) as Jul,
       sum(case when month = 'Aug' then total else 0 end) as Aug,
       sum(case when month = 'Sep' then total else 0 end) as Sep,
       sum(case when month = 'Oct' then total else 0 end) as Oct,
       sum(case when month = 'Nov' then total else 0 end) as Nov,
       sum(case when month = 'Dec' then total else 0 end) as `Dec`
from your_table
group by name

关于php - 选择名称相似的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756927/

相关文章:

java - 专用 MySql 服务器与应用程序引擎配合使用

C 套接字 - 并非所有套接字都在 select() 中接收

php - Twig date time_diff 翻译

javascript - 迭代 JS 对象/数组,如果找到匹配项,则更改值并返回原始对象/数组结构

mysql - GROUP BY 附近的 SQL 语法错误

mysql - 使用 join、group by 和 Sum() 的 LINQ 查询

mysql - 从 SELECT 查询中排除起始词

MySQL - 左连接并从不在另一个表中的表中选择行

PHP 正则表达式和 CSV 验证

php - 如何加快我的 PHP 包含速度?