1. 利用max(case when then)
聚合函数,max,取最大值
(case when course = '语文' then score else 0 end) ---判断
as 语文---别名作为列名
2.max(when course='语文' then score else 0 end)
mysql> SELECT -> `name`, -> MAX( -> CASE -> WHEN course='语文' THEN -> score else 0 -> END -> ) AS 语文, -> MAX( -> CASE -> WHEN course='数学' THEN -> score else 0 -> END -> ) AS 数学, -> MAX( -> CASE -> WHEN course='英语' THEN -> score else 0 -> END -> ) AS 英语 -> FROM -> student -> GROUP BY `name` -> ;
参考:https://www.jb51.net/article/109487.htm