给定:
id | a | b | c
---------------
1 | 3 | 8 | 7
2 | 4 | 5 | 6
3 | 0 | 8 | 1
4 | 2 | 4 | 6
5 | 6 | 5 | 4
6 | 9 | 5 | 2
7 | 0 | 1 | 8
就像彩票一样,我想选择所有具有相同组合的行,而不考虑顺序。预期的选择应该给我
id | a | b | c
---------------
2 | 4 | 5 | 6
5 | 6 | 5 | 4
3 | 0 | 8 | 1
7 | 0 | 1 | 8
我试过自己加入表格,不确定这是否是正确的方法。这是 postgres
最佳答案
您可以使用 exists
进行过滤:
select t.*
from mytable t
where exists (
select 1
from mytable t1
where
t1.id <> t.id
and t1.a in (t.a, t.b, t.c)
and t1.b in (t.a, t.b, t.c)
and t1.c in (t.a, t.b, t.c)
)
id | a | b | c -: | -: | -: | -: 5 | 6 | 5 | 4 7 | 0 | 1 | 8 2 | 4 | 5 | 6 3 | 0 | 8 | 1
关于sql - 无论顺序如何,选择具有相同集合的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594213/