如何转换以下5列结构的表格:
Id, name, col1, col2, col2
1 aaa 10 20 30
2 bbb 100 200 300
到以下结构,其中Col1,Col2和Col3列现在在新列Colx中显示为字符串。
Id, name, Colx, Value
1 aaa Col1 10
1 aaa Col2 20
1 aaa Col3 30
2 bbb Col1 100
2 bbb Col2 200
2 bbb Col3 300
谢谢! 阿维
答案 0 :(得分:0)
您可以将子查询与UNION语句
一起使用select nombre, colx, val from (
select nombre, 'col1' as colx, col1 as val from test
UNION
select nombre, 'col2' as colx, col2 as val from test
UNION
select nombre, 'col3' as colx, col3 as val from test
) as query
order by val