首页 > 解决方案 > Sql查询将所有列合并为一列

问题描述

  id college1  college2  college3
   1 abc       xyz        rst

以上是输入-

寻找输出:-

id college1  college2  college3
 1  abc 
 1  xyz
 1  rst

标签: mysqlsqlsql-query-store

解决方案


一种方法很简单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;

这需要扫描表 3 次,通常没问题。但是,如果“t”确实是一个复杂的查询或非常大的表,那么还有更有效的方法。


推荐阅读