当前位置:Gxlcms > 数据库问题 > SQL行转列 静态?

SQL行转列 静态?

时间:2021-07-01 10:21:17 帮助过:3人阅读

转换前的 :
ID name sex num
1 tom 男 2
2 tom 男 3
3 tom 男 4
4 tom 男 5
这是转换后的结果
ID name sex num1 num2 num3 num4
1 tom 男 2 3 4 5



select MIN(id) as ID, name, sex, 
sum(case when num=2 then 2 end) as num2,
sum(case when num=3 then 3 end) as num3,
sum(case when num=4 then 4 end) as num4,
sum(case when num=5 then 5 end) as num5
from 表名 group by name,sex

SQL行转列 静态?

标签:

人气教程排行