[英]MySql How to Merge two columns in one column Such that second column ouput goes uder first column in multiple rows
How to Merge two columns in one column Such that second column ouput goes uder first column in multiple rows without using UNION in single query.如何合并一列中的两列这样第二列输出会在多行中的第一列下进行,而无需在单个查询中使用 UNION。
for Eg:例如:
column1 column2
1 3
2 4
expected OP:预期操作:
column1
1
2
3
4
without using UNION in single query
在单个查询中不使用 UNION
SELECT DISTINCT CASE WHEN t00.id = t01.id
THEN t1.column1
ELSE t2.column2
END all_values
FROM ( SELECT column1 id
FROM test
ORDER BY 1
LIMIT 2 ) t00
CROSS JOIN ( SELECT MIN(column1) id
FROM test ) t01
CROSS JOIN test t1
CROSS JOIN test t2
ORDER BY 1
The table must have at least 2 rows.该表必须至少有 2 行。
Applicable for 5.x MySQL version.适用于 5.x MySQL 版本。
union all
is the right approach: union all
是正确的方法:
select column1 as col
from t
union all
select column2
from t
order by col;
You can also use a cross join
with the union all
in a subquery:您还可以在子查询中使用带有
union all
的cross join
:
select (case when seqnum = 1 then column1 else column2 end) as col
from t cross join
(select row_number() over () as seqnum
from t
limit 2
) tt
order by col;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.