我的问题很简单,就是想把两张表拼成一张,不PK 第一张表完全不同,它们完全不同
table1. table2.
|в|q| |@|John |
|ы|a| |£|Sara |
|в|f| |$|ciro |
|с|g| |%|Jo. |
|ф|s|
我需要的是这个
Table3
|в|q|@|John |
|ы|a|£|Sara |
|в|f|$|ciro |
|с|g|%|Jo. |
|ф|s|-|- |
最佳答案
这有点复杂。您想要一个“垂直”列表,但没有任何内容可以匹配列。您可以使用 row_number()
和 union all
:
select max(t1_col1), max(t1_col2), max(t2_col1), max(t2_col2)
from ((select t1.col1 as t1_col1, t1.col2 as t1_col2,
null as t2_col1, null as t2_col2, row_number() over () as seqnum
from table1 t1
) union all
(select null, null, t2.col1, t2.col2, row_number() over () as seqnum
from table2 t2
)
) t
group by seqnum;
Here是一个数据库<> fiddle 。
请注意,这将保留两个表中的所有行,无论哪个更长。每列中的行的特定顺序是不确定的。 SQL 表表示无序 集。如果您想要按特定顺序排列的事物,则需要一个指定顺序的列。
如果要将其保存在新表中,请将 create table as table3
放在 select
之前。如果要插入到现有表中,请使用 insert
。
关于mysql - 两张 table 合二为一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58917520/