我的表
ANONYMOUS
ONE TWO
1 2
2 1
1 2
3 1
现在我想选择一个和两个不同的集合。 我选择的列表应为
ANONYMOUS
ONE TWO
1 2
3 1
答案 0 :(得分:3)
你的问题不是很明确,但我想你的意思是:
SELECT DISTINCT one, two
FROM yourtable AS T1
WHERE one <= two
OR NOT EXISTS
(
SELECT *
FROM yourtable AS T2
WHERE T1.one = T2.two
AND T1.two = T2.one
)
它找到(one, two)
的行,其中反向对(two, one)
不存在。如果两者都存在,则选择one < two
对。它还选择值相等的行。
查看在线工作:sqlfiddle
如果您希望使用JOIN
代替NOT EXISTS
,则可以这样做:
SELECT DISTINCT T1.one, T1.two
FROM yourtable AS T1
LEFT JOIN yourtable AS T2
ON T1.one = T2.two
AND T1.two = T2.one
WHERE T1.one <= T1.two
OR T2.one IS NULL
查看在线工作:sqlfiddle
答案 1 :(得分:0)
SELECT DISTINCT a.*
FROM `ANONYMOUS` a
LEFT JOIN `ANONYMOUS` b ON (a.one=b.two and a.two=b.one)
WHERE b.one is null or a.one<b.one
ORDER BY 1,2