我有一个看起来像这样的表:
+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 1 | 2 | 3 |
| 2 | 3 | 4 |
| 3 | 4 | 5 |
+------+------+------+
我想写一个查询来得到这个:
+------+
| Col1 |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
我的问题是:是否可以在不使用 union
的情况下将所有列合并为一个列?
最佳答案
嗯,是的。但它并不优雅:
select distinct (case n when 1 then col1 when 2 then col2 when 3 then col3) as col
from t cross join
(select (@rn := @rn + 1) as n
from t cross join
(select @rn := 0) params
limit 3
) n;
关于mysql - 如何将所有列合并为一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255266/