简体   繁体   中英

Find the rows with same values of different columns in SQL

I am working with a table [user] which has following columns

user_id | em_id | em_id_original
--------|-------|---------------
1       | 32    |    NULL
2       | 43    |    32
3       | 14    |    13
4       | 75    |    98
5       | 98    |    NULL
...

Now, I want to find the pairs of rows for which the values of two columns em_id and em_id_original as same. So, if you run the query on the table above, it should generate following output.

user_id | em_id | em_id_original
--------|-------|---------------
1       | 32    |    NULL
2       | 43    |    32
4       | 75    |    98
5       | 98    |    NULL

first pair (user_id 1 and 2): em_id = em_id_original = 32

second pair (user_id 4 and 5): em_id = em_id_original = 98

Thank you

You can use correlated subqueries and EXISTS to check if either counterpart for a row exists.

SELECT u1.user_id,
       u1.em_id,
       u1.em_id_original
       FROM `[user]` u1
       WHERE EXISTS (SELECT *
                            FROM `[user]` u2
                            WHERE u2.em_id = u1.em_id_original)
              OR EXISTS (SELECT *
                                FROM `[user]` u2
                                WHERE u2.em_id_original = u1.em_id);

It appears to me you can do this with a simple subquery, having said that I've had to jump to some conclusions regarding your dataset, if this incorrect you may want to add some more exceptions to your data so we can better understand your model.

SELECT user_id, em_id, em_id_original
FROM [user]
WHERE em_id_original IS NULL OR em_id_original IN (SELECT em_id FROM [user])

With a self join:

select distinct u1.*
from `user` u1 inner join `user` u2
on u2.em_id_original = u1.em_id or u1.em_id_original = u2.em_id 
order by u1.user_id

You can remove distinct if there is no case a em_id to appear more than once in u1.em_id_original and vice versa.
See the demo .

| user_id | em_id | em_id_original |
| ------- | ----- | -------------- |
| 1       | 32    | NULL           |
| 2       | 43    | 32             |
| 4       | 75    | 98             |
| 5       | 98    | NULL           |

You can try this

SELECT * 
FROM test t
WHERE EXISTS (  SELECT 'a'
                FROM test t2
                WHERE t2.em_id = t.em_id_original
            )
OR EXISTS (  SELECT 'a'
             FROM test t2
             WHERE t2.em_id_original = t.em_id 
           )

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM