简体   繁体   English

Sql 查询将所有列合并为单个列

[英]Sql query to merge all columns into a single column

  id college1  college2  college3
   1 abc       xyz        rst

Above is the input-以上是输入-

Looking for output:-寻找 output:-

id college1  college2  college3
 1  abc 
 1  xyz
 1  rst

One method is simply union all :一种方法是简单地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;

This requires scanning the table three times, which is usually fine.这需要扫描表 3 次,通常没问题。 But if "t" is really a complicated query or really big table, there are more efficient approaches.但是,如果“t”确实是一个复杂的查询或非常大的表,那么还有更有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM