假设我有这两个表(table1的列id,table2的列字母):
+ ----- +
| id |
+ ----- +
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+ ----- +
+ --------- +
|字母|
+ --------- +
| a |
| b |
| c |
| d |
| e |
+ --------- +
我如何将这两张桌子连在一起以便显示以下内容?:
+ ----- + --------- +
| id |字母|
+ ----- + --------- +
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
+ ----- + --------- +
我正在使用postgreSQL-9.2。
答案 0 :(得分:1)
在加入条件中使用row_number()
函数:
select id, letter
from (select id, row_number() over (order by 1) as rn from ids) i
join (select letter, row_number() over (order by 1) as rn from letters) l
on i.rn = l.rn;