Mysql行转列,列转行 WITH ROLLUP 统计 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id INT(11) NOT NULL AUTO_INCREMENT, userid VARCHAR(20) NOT NULL COMMENT '用户id', SUBJECT VARCHAR(20) COMMENT '科目', score DOUBLE COMMENT '成绩', PRIMARY KEY(id) )ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('001','语文',90); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('001','数学',92); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('001','英语',80); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('002','语文',88); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('002','数学',90); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('002','英语',75.5); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('003','语文',70); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('003','数学',85); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('003','英语',90); INSERT INTO tb_score(userid,SUBJECT,score) VALUES ('003','政治',82); SELECT * FROM tb_score ## 行转列 WITH ROLLUP 统计 # 1、使用case...when....then 进行行转列 SELECT userid, SUM(CASE `subject` WHEN '语文' THEN score ELSE 0 END) AS '语文', SUM(CASE `subject` WHEN '数学' THEN score ELSE 0 END) AS '数学', SUM(CASE `subject` WHEN '英语' THEN score ELSE 0 END) AS '英语', SUM(CASE `subject` WHEN '政治' THEN score ELSE 0 END) AS '政治' FROM tb_score GROUP BY userid # 2、使用IF() 进行行转列: SELECT userid, SUM(IF(`subject`='语文',score,0)) AS '语文', SUM(IF(`subject`='数学',score,0)) AS '数学', SUM(IF(`subject`='英语',score,0)) AS '英语', SUM(IF(`subject`='政治',score,0)) AS '政治' FROM tb_score GROUP BY userid # 3、利用SUM(IF()) 生成列 + WITH ROLLUP 生成汇总行,并利用 IFNULL将汇总行标题显示为Total SELECT IFNULL(userid,'total') AS userid, SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治, SUM(IF(`subject`='total',score,0)) AS total FROM( SELECT userid,IFNULL(`subject`,'total') AS `subject`,SUM(score) AS score FROM tb_score GROUP BY userid,`subject` WITH ROLLUP HAVING userid IS NOT NULL )AS A GROUP BY userid WITH ROLLUP; ## 行统计 SELECT userid, SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治, SUM(IF(`subject`='total',score,0)) AS total FROM( SELECT userid,IFNULL(`subject`,'total') AS `subject`,SUM(score) AS score FROM tb_score GROUP BY userid,`subject` WITH ROLLUP HAVING userid IS NOT NULL )AS A GROUP BY userid WITH ROLLUP HAVING userid IS NOT NULL ## 列统计 SELECT IFNULL(userid,'total') AS userid, SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治 FROM( SELECT userid,IFNULL(`subject`,'total') AS `subject`,SUM(score) AS score FROM tb_score GROUP BY userid,`subject` )AS A GROUP BY userid WITH ROLLUP # 4、利用SUM(IF()) 生成列 + UNION 生成汇总行,并利用 IFNULL将汇总行标题显示为 Total SELECT userid, SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治, SUM(score) AS TOTAL FROM tb_score GROUP BY userid UNION SELECT 'TOTAL',SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治, SUM(score) FROM tb_score ## 5、利用SUM(IF()) 生成列,直接生成结果不再利用子查询 SELECT IFNULL(userid,'TOTAL') AS userid, SUM(IF(`subject`='语文',score,0)) AS 语文, SUM(IF(`subject`='数学',score,0)) AS 数学, SUM(IF(`subject`='英语',score,0)) AS 英语, SUM(IF(`subject`='政治',score,0)) AS 政治, SUM(score) AS TOTAL FROM tb_score GROUP BY userid WITH ROLLUP; ## 6、动态,适用于列不确定情况 ?? 以下语句需要一行一行执行 SET @EE=''; SELECT @EE :=CONCAT(@EE,'sum(if(subject= \'',SUBJECT,'\',score,0)) as ',SUBJECT, ',') AS aa FROM (SELECT DISTINCT SUBJECT FROM tb_score) A ; SET @QQ = CONCAT('select ifnull(userid,\'TOTAL\')as userid,',@EE,' sum(score) as TOTAL from tb_score group by userid WITH ROLLUP'); -- SELECT @QQ; PREPARE stmt FROM @QQ; EXECUTE stmt; DEALLOCATE PREPARE stmt; ## 7、合并字段显示:利用group_concat() SELECT userid,GROUP_CONCAT(`subject`,":",score)AS 成绩 FROM tb_score GROUP BY userid ## 结论:group_concat()函数可以很好的建属于同一分组的多个行转化为一个列。 ## 列转行 CREATE TABLE tb_score1( id INT(11) NOT NULL AUTO_INCREMENT, userid VARCHAR(20) NOT NULL COMMENT '用户id', cn_score DOUBLE COMMENT '语文成绩', math_score DOUBLE COMMENT '数学成绩', en_score DOUBLE COMMENT '英语成绩', po_score DOUBLE COMMENT '政治成绩', PRIMARY KEY(id) )ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('001',90,92,80,0); INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('002',88,90,75.5,0); INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('003',70,85,90,82); SELECT * FROM tb_score1 SELECT userid,'语文' AS course,cn_score AS score FROM tb_score1 UNION ALL SELECT userid,'数学' AS course,math_score AS score FROM tb_score1 UNION ALL SELECT userid,'英语' AS course,en_score AS score FROM tb_score1 UNION ALL SELECT userid,'政治' AS course,po_score AS score FROM tb_score1 ORDER BY userid ## 写在最后,不然也可以查询出来。不过结果未排序 /** UNION与UNION ALL的区别(摘): 1.对重复结果的处理:UNION会去掉重复记录,UNION ALL不会; 2.对排序的处理:UNION会排序,UNION ALL只是简单地将两个结果集合并; 3.效率方面的区别:因为UNION 会做去重和排序处理,因此效率比UNION ALL慢很多; 转载: https://www.cnblogs.com/xiaoxi/p/7151433.html **/