首页 > 解决方案 > 如何在 SQL 中查找表列的唯一组合?

问题描述

我有下表:测试

create table test (Id char)

insert into test values
('A'),
('B'),
('C'),
('D'),
('E')

用于测试的小提琴链接

预期输出:

  1. a.ID b.ID

我尝试了以下代码:

select a.id, b.id from test a cross join test b where a.id<>b.id

但是我当前的输出有无效的组合,上面已经存在参考图像 here(突出显示的记录无效,因为它们以相反的顺序出现在上面)

标签: sqloracle

解决方案


应该相当简单;从切换<><

select a.id, b.id from test a cross join test b where a.id < b.id

推荐阅读