Skip to content
Advertisement

Compare values in Different column and row

I have the following table:

ID COl1 COl2
1  13    15
2  13    16
3  13    17
4  17    13

What I need is to select all rows where Col1 value is available in Col2 and vice versa.

This case only ROW 4 or ROW 3 should be returned. They have same values (13 17).

Take it as col1 is Buyer and col2 is Seller

I want to know who are the users who bought / sell from EACH OTHER. if user a bought from user b, user b should buy from user a in order to be returned.

Advertisement

Answer

SELECT
    a.*
FROM
    yourTable   a
INNER JOIN
    yourTable   b
        ON  a.Col1 = b.Col2
        AND a.Col2 = b.Col1
        AND a.id  != b.id
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement