一尘不染

sql并排合并表,没有任何共同点

sql

我正在寻找有关如何合并两个没有任何共同点的表的sql答案。

So let's say you have these two tables without anything in common:

Guys                Girls
id    name          id   name
---  ------        ----  ------
1     abraham        5    sarah
2     isaak          6    rachel
3     jacob          7    rebeka
                     8    leah

and you want to merge them side-by-side like this:

Couples

id   name      id   name
---  ------    ---  ------
1    abraham   5    sarah
2    isaak     6    rachel
3    jacob     7    rebeka
               8    leah

How can this be done?

我正在寻找有关如何合并两个没有任何共同点的表的sql答案。


阅读 124

收藏
2021-03-17

共1个答案

一尘不染

您可以通过创建一个键(即行号)并加入该键来完成此操作。

SQL的大多数方言都支持该row_number()功能。这是一种使用它的方法:

select gu.id, gu.name, gi.id, gi.name
from (select g.*, row_number() over (order by id) as seqnum
      from guys g
     ) gu full outer join
     (select g.*, row_number() over (order by id) as seqnum
      from girls g
     ) gi
     on gu.seqnum = gi.seqnum;
2021-03-17