1 数据表:
列转行:利用max(case when then)
max---聚合函数 取最大值
(case course when 语文 then score else 0 end) ---判断
as 语文---别名作为列名
SELECT `name`, MAX( CASE WHEN course=语文 THEN score END ) AS 语文, MAX( CASE WHEN course=数学 THEN score END ) AS 数学, MAX( CASE WHEN course=英语 THEN score END ) AS 英语 FROM student GROUP BY `name` ;
2 合并字段显示:利用group_cancat(course,”:”,”score”)
SELECT `name`, GROUP_CONCAT(course, ":", score) AS 成绩 FROM student GROUP BY `name`;
group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。 1.测试语句:
SELECT GROUP_CONCAT(`name`) FROM student GROUP BY `name`;