轉(zhuǎn)載地址:https://www.cnblogs.com/thomas12112406/p/5813394.html
數(shù)據(jù)表:
列轉(zhuǎn)行:利用max(case when then)
`max``---聚合函數(shù) 取最大值`
`(`casecourse when
'語文'
then
score else
0 end
)
---判斷`
` as 語文``---別名作為列名`
SELECT `name`,
MAX( CASE WHEN course='語文' THEN score END ) AS 語文,
MAX( CASE WHEN course='數(shù)學' THEN score END ) AS 數(shù)學,
MAX( CASE WHEN course='英語' THEN score END ) AS 英語
FROM student GROUP BY `name`;