Look for interlaced ID (which is mutual), sql mysql

1

I have a table of this format

RELATIONS
-----------------
ID | ID_1 | ID_2

All in INT and ID is auto incremental.

It is filled in this way:

ID | ID_1 | ID_2
----------------- 
1  |  1   |  5
-----------------
2  |  5   |  1
-----------------
3  |  1   |  3
-----------------
4  |  3   |  6
-----------------
5  |  6   |  3
-----------------

Have you noticed that there are interlaced IDS? The 1 and the 5 are connected on both the first and second paths, 3 and 6 also but 1 and 3 no, because the connections are not mutual ..

How to get the interleaves (which are mutual on the first and second path) of the id number 6 ? or 5 , or 1 ?

For example, I want to get the ID of the interleaving of ID 6

ID_PROCURADO | ENTRELAÇADO
---------------------------
6            |  3
    
asked by anonymous 02.07.2015 / 19:10

1 answer

2

I believe it to be this:

select t1.id_1 as procurado, t1.id_2 as entrelacado
from teste t1
inner join teste t2 on t1.id_1 = t2.id_2 
                   and t1.id_2 = t2.Id_1
where t1.id_1 = 6

Due to INNER JOIN will only return the interlaced.

SqlFiddle Example

    
02.07.2015 / 19:56