假设查询结果应该返回字符串对(x,y)的列表.我试图消除反向重复.我的意思是,如果(x,y)是结果之一,(y,x)不应该在以后出现.
例:
column 1 (foreign key) column 2 (int) column 3 (string)
4 50 Bob
2 70 Steve
3 50 Joe
Run Code Online (Sandbox Code Playgroud)
此表中显示的人可以多次出现,具有不同的第2列值.
我的查询需要打印具有相同列2值的每对名称:
select e.column3, f.column3 from example as e, example as f where e.column2 = f.column2
(Bob, Bob)
(Bob, Joe)
(Joe, Bob)
(Joe, Joe)
Run Code Online (Sandbox Code Playgroud)
我升级了查询,以便删除双打:
select e.column3, f.column3 from example as e, example as f where e.column2 = f.column2
and e.column3 <> f.column3
(Bob, Joe)
(Joe, Bob)
Run Code Online (Sandbox Code Playgroud)
现在我希望它只返回:
(Bob, Joe).
Run Code Online (Sandbox Code Playgroud)
(乔,鲍勃)是一个反向复制,所以我不希望它在结果中.无论如何在一个查询中处理它?
Ric*_*iwi 26
首先,欢迎来到2012年.我们已经使用逗号从相关表中迁移出来.它在ANSI 89中被引入,但严重缺乏.现在,正确的方法是使用ANSI 92/99/2003 JOIN语法编写查询.
问题的解决方案是将双向不等式<>
转换为单向不等式,无论是您喜欢的<
还是其中>
任何一个.
select e.column3, f.column3
from example as e
join example as f on e.column2 = f.column2 and e.column3 < f.column3
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12794 次 |
最近记录: |