隐式列到SQL中的行

时间:2019-06-11 07:36:56

标签: mysql sql

我有一个包含多列的表,如下所示:

+-------+------+------+------+
| Name  | col1 | col2 | col3 |
+-------+------+------+------+
| pinki | a    | b    | c    |
+-------+------+------+------+

如何将列转换为行并实现如下输出表-

+-------+-----+
| Name  | col |
+-------+-----+
| Pinki | a   |
| Pinki | b   |
| Pinki | c   |
+-------+-----+

测试表-

create table testing(
Name varchar(10),
col1 varchar(10),
col2 varchar(10),
col3 varchar(10));

insert into testing(Name,col1,col2,col3) values ('pinki','a','b','c');

1 个答案:

答案 0 :(得分:1)

您可以使用union all

select name, col1 as col from testing
union all
select name, col2  from testing
union all
select name, col3  from testing