开发者

MySQL pivot single row

开发者 https://www.devze.com 2023-02-04 18:37 出处:网络
I\'ve found a lot of questions to do this the other way but I can\'t solve it for this way. Let\'s say I select one row from a table so I have the following:

I've found a lot of questions to do this the other way but I can't solve it for this way.

Let's say I select one row from a table so I have the following:

A | B | C | D | E
------------------
 1| 2 | 3 | 4 开发者_高级运维| 5

How can I pivot it in mysql to create something like:

r | c
-----
A | 1
-----
B | 2
-----
C | 3
-----
D | 4
-----
E | 5

which I then plan to join onto another table.

Is this possible to do? How? I can't think of any ways and I can't find an answer on google.

Thank you.


If your column names are fixed, you can use this

(Sample table)

create table so_tmp(a int, b int, c int, d int, e int);
insert into so_tmp select 1,2,3,4,5;

select a,b,c,d,e from so_tmp;

Unpivot query:

select 'a' r, a c from so_tmp
union all
select 'b', b from so_tmp
union all
select 'c', c from so_tmp
union all
select 'd', d from so_tmp
union all
select 'e', e from so_tmp;
0

精彩评论

暂无评论...
验证码 换一张
取 消