0

以下是我所拥有的

+++++++++++++++++
+id1    +   id2 +
+++++++++++++++++
+  1    +    2  +
+  2    +    1  +
+  1    +    3  +
+  3    +    2  +
+  4    +    5  +
+  5    +    4  +
+++++++++++++++++

我要查找的是两个列中的两个人的 ID。

下面是我将拥有的

+++++++++++++++++
+id1    +   id2 +
+++++++++++++++++
+  1    +    2  +
+  2    +    1  +
+  4    +    5  +
+  5    +    4  +
+++++++++++++++++

或者

+++++++++++++++++
+id1    +   id2 +
+++++++++++++++++
+  1    +    2  +
+  4    +    5  +
+++++++++++++++++

或者

+++++++++++++++++
+id1    +   id2 +
+++++++++++++++++
+  2    +    1  +
+  5    +    4  +
+++++++++++++++++

知道如何实现这一目标吗?

这是测试数据

4

2 回答 2

0

这应该让你开始:

SELECT
    mytable1.id1, mytable1.id2
FROM
    myTable mytable1,
    myTable mytable2
WHERE
    mytable1.id1 = mytable2.id2
    AND
    mytable2.id1 = mytable1.id2
ORDER BY
    mytable1.id1

演示

于 2012-06-11T13:27:01.557 回答
0

基本的 id 列表是这样的:

select distinct t1.id1
from mytable t1
join mytable t2 on t1.id1 = t2.id2;

如果你想要

select distinct t1.*
from mytable t1
join mytable t2 on t1.id1 = t2.id2;

如果你想要任何 2 行:

select distinct t1.*
from mytable t1
join mytable t2 on t1.id1 = t2.id2
limit 2;
于 2012-06-11T13:34:53.620 回答