sql - 无论顺序如何,选择具有相同集合的行

标签 sql postgresql

给定:

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)
)

Demo on DB Fiddle :

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/

相关文章:

c# - 无法找到列

windows - PostgreSQL 9 安装在 Windows : "Unable to write inside TEMP environment path."

php - sql插入语句有时工作其他时间不工作

sql - Rails 3.2 ActiveRecord SQL 语法错误

postgresql - 串行类型列值获取非连续值

python - 为什么带有 Docker 的 Django 服务器在 postgresql 中运行时没有数据库?

postgresql - 将行插入临时表

sql - PostgreSQL 返回 id 到数组

c# - 替代 Group_Concat 以自定义现有 Entity Framework 模型

c# - 从 ASP.NET 标识中删除 `AspNetUserLogins` 表