行转列
select
`username`,
max(if(`subject`='语文',score,0)) as '语文',
max(if(`subject`='数学',score,0)) as '数学',
max(if(`subject`='英语',score,0)) as '英语'
from score group by username;
- 转换前后效果
列转行
select `username`,'语文' as 'subject',`cn` as 'score' from score1
union all
select `username`,'数学' as 'subject',`math` as 'score' from score1
union all
select `username`,'英语' as 'subject',`en` as 'score' from score1
- 转换前后效果
参考:
https://blog.csdn.net/lwj19921222/article/details/108654043
https://database.51cto.com/art/202106/667755.htm
版权声明:本文为CSDN博主「Eric-liang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lwj19921222/article/details/108654043