mysql - Sql查询将所有列合并为一列
问题描述
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;
这需要扫描表 3 次,通常没问题。但是,如果“t”确实是一个复杂的查询或非常大的表,那么还有更有效的方法。
推荐阅读
- r - 从一系列文件名中提取具有可变结构的日期,并将其作为日期变量 (R) 添加到源栅格中
- javascript - 我试图制作一个以字母作为输入的代码,并给出一系列以该字母为星标的国家/地区(JS)
- django - 如何将 Django 与 Tornado 网络套接字一起使用?
- javascript - React Hooks: useState updater function: 为什么这个钩子会在拖动时删除对象?
- sql-server - 从 SQL 查询监视 Power BI 刷新
- php - Magento 2 自定义 REST API 多部分/表单数据
- java - 在 Java.time 中标准化 LocalDate.getDayOfWeek().getValue()
- react-native - 当我使用抽屉内容时,抽屉屏幕未显示在本机应用程序中
- c# - 对象到 xml 并在不同的字段中添加不同的前缀和命名空间
- c# - C# PS Cmdlet - 无法加载文件或程序集或其依赖项之一。系统找不到文件