动态,适用于列不确定情况,第一种: SET @EE=''; SELECT GROUP_CONCAT('SUM(IF(C2=\'',C2,'\'',',C3,0)) AS ',C2)INTO @EE FROM (SELECT DISTINCT C2 FROM TX) A; SET @QQ=CONCAT('SELECT ifnull(c1,\'total\') as ''合计'',',@EE,' ,SUM(C3) AS TOTAL FROM TX GROUP BY C1 WITH ROLLUP');…