【问题标题】:Sql query to merge all columns into a single columnSql查询将所有列合并为一列
【发布时间】:2020-06-09 14:37:20
【问题描述】:
  id college1  college2  college3
   1 abc       xyz        rst

上面是输入-

寻找输出:-

id college1  college2  college3
 1  abc 
 1  xyz
 1  rst

【问题讨论】:

  • 为什么你的结果集有四列?

标签: mysql sql sql-query-store


【解决方案1】:

一种方法就是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" 真的是一个复杂的查询或非常大的表,还有更有效的方法。

【讨论】:

  • 谢谢你的回答,这将适用于大学专栏,但是如何获得id专栏
  • @user190549 。 . .结果集同时具有idcollege
猜你喜欢
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多