1

我的桌子

ANONYMOUS
ONE   TWO
1      2
2      1
1      2
3      1

现在我想选择一个和两个不同的集合。我选择的列表应该是

ANONYMOUS
    ONE   TWO
    1      2
    3      1
4

2 回答 2

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

如果您更愿意使用 aJOIN而不是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

于 2012-06-23T16:23:50.733 回答
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
于 2012-06-23T17:43:04.390 回答