MySQL行列转换

2017-10-09  本文已影响96人  我念东风终不负

score_1

id course score
1 数学 99
1 英语 89
1 法律 98
2 数学 100
2 英语 99
2 法律 87

score_2

id 数学 英语 法律
1 99 89 98
2 100 99 87

RT,需求是将以上两种表样式互相转换:

score_1转score_2,很简单:

select ID
    ,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 score_1
group by id;

score_2转score_1:

未完待续

上一篇 下一篇

猜你喜欢

热点阅读