PostgreSQL,如何提取表中所有可能的主键对?

Dav*_*.it 1 postgresql duplication

我想找出一个 PostgreSQL 命令,它读取一个表的内容,并返回该表的主键的所有可能的耦合。

像这样的东西:

input_table(ID为主键):

- - - - - - - - - - 
ID | label | name
- - - - - - - - - - 
11 | aaa | Jenny

24 | bbb | Larry

35 | ccc | Trevor

48 | ddd | Jules
- - - - - - - - - - 
Run Code Online (Sandbox Code Playgroud)

查询应该返回主键的所有可能的耦合:

- - - - - - - - - - 
ID_left | ID_right
- - - - - - - - - - 
11 | 24

11 | 35

11 | 48

24 | 35

24 | 48

35 | 48
- - - - - - - - - - 
Run Code Online (Sandbox Code Playgroud)

是否可以设计此查询?如果是,如何?

谢谢

a_h*_*ame 6

听起来像一个简单的交叉连接:

select a.id, b.id
from input_table a
  cross join input_table b
where a.id <> b.id;
Run Code Online (Sandbox Code Playgroud)