找到两列中的两个人ID

时间:2012-06-11 13:18:12

标签: mysql

以下是我的内容

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

我想要找到的是同时在两栏中的两个人的身份。

以下是我的内容

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

或者

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

或者

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

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

Here is testing data

2 个答案:

答案 0 :(得分: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

Demo

答案 1 :(得分: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;