我正在寻找有关如何合并两个没有任何共同点的表的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的大多数方言都支持该row_number()功能。这是一种使用它的方法:
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;