所以我有一个像这样的 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/