简体   繁体   中英

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:-

id college1  college2  college3
 1  abc 
 1  xyz
 1  rst

One method is simply 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. But if "t" is really a complicated query or really big table, there are more efficient approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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