php – 如何在MySQL ROLLUP函数中替换NULL类别标题?

前端之家收集整理的这篇文章主要介绍了php – 如何在MySQL ROLLUP函数中替换NULL类别标题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用下面的 MySQL查询,我创建了一个数据透视表,这正是我正在寻找的.但是,我想用实际描述替换NULL值,例如SubTotal和GrandTotal.这是我的 PHP输出显示的数据透视表格式(希望格式化有点清晰!).
Name    Division 1  Division 2  Division 3  Division 4  Location
Name 1
Name 2
Name 3
NULL    Total       Total       Total       Total
Name 4
Name 5
NULL    Total       etc
NULL    Column Grand Total

这是我用来生成表的查询.在研究了这个问题后,CASE功能似乎就是这样.但是,当我将两个CASE行添加到下面的查询中时,它似乎不想工作.返回的MysqL_error表示“GROUPING函数不存在”.

SELECT 
CASE WHEN (GROUPING(name)=1) THEN 'MainTotal' ELSE name END AS name,CASE WHEN (GROUPING(location)=1) THEN 'SubTotal' ELSE location END AS location,name AS Name,SUM(IF(division='OEM',totalHours,NULL)) AS OEM,SUM(IF(division='A/M',NULL)) AS AM,SUM(IF(division='SKF',NULL)) AS SKF,SUM(IF(division='RE',NULL)) AS RE,location as Location
FROM $databasetable GROUP BY location,name 
WITH ROLLUP

谁能告诉我我做错了什么? CASE函数是否可以替换NULL类别标题

提前致谢!

试试这样:
SELECT 
IFNULL(name,'MainTotal') AS name,IFNULL(location,'SubTotal') AS location,name 
WITH ROLLUP
原文链接:https://www.f2er.com/php/134924.html

猜你在找的PHP相关文章