mysql 行列转换怎么写?

mysql 行列转换怎么写?,第1张

set @i =0

create table 新表

AS

select @i:=@i+1 as NO,NO1,NO2,NO3,MYA1 as MM

from(

select NO1,NO2,'MYA1 ' as NO3,MYA1 from 表

union all

select NO1,NO2,'MYA2 ',MYA2 from 表

union all

select NO1,NO2,'MYA3 ',MYA3 from 表

union all

select NO1,NO2,'MYA4 ',MYA4 from 表

union all

select NO1,NO2,'MYA5 ',MYA5 from 表

union all

select NO1,NO2,'MYA6 ',MYA6 from 表

)a order by NO1,NO2

select * from 新表

原表:

?

1

2

3

4

5

6

7

8

9

10

11

12

表名 :user

----------------------------------------

name| course | grade

----------------------------------------

zhangsan | Java | 70

----------------------------------------

zhangsan | C++ | 80

----------------------------------------

lisi| java | 90

----------------------------------------

lisi| C#| 60

----------------------------------------

用一条 SQL 语句得到如下形式

?

1

2

3

4

5

6

7

----------------------------------------

name | java | C++ | C#

----------------------------------------

zhangsan | 70 | 80 | null

----------------------------------------

lisi | 90 | null | 60


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8674562.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存