Sql 将所有列合并为一个列的查询
Sql query to merge all columns into a single column
id college1 college2 college3
1 abc xyz rst
以上是输入-
正在寻找输出:-
id college1 college2 college3
1 abc
1 xyz
1 rst
一种方法就是 union all
:
select id, college1 as college from t
union all
select id, college2 as college from t
union all
select id, college3 as college from t;
这需要扫描table三遍,这通常没问题。但是如果 "t" 真的是一个复杂的查询或者非常大的查询 table,还有更有效的方法。
id college1 college2 college3
1 abc xyz rst
以上是输入-
正在寻找输出:-
id college1 college2 college3
1 abc
1 xyz
1 rst
一种方法就是 union all
:
select id, college1 as college from t
union all
select id, college2 as college from t
union all
select id, college3 as college from t;
这需要扫描table三遍,这通常没问题。但是如果 "t" 真的是一个复杂的查询或者非常大的查询 table,还有更有效的方法。