简体   繁体   中英

MySQL Select rows with same FK but different values in other row

I am going back through and auditing some database entries and need a query to find the rows I need to work on.

I have tried a few subqueries but I could not get near what I'm looking for.

An example table would be:

code_id     id     exchange
1           23     5
2           23     5
3           24     5
4           24     7
5           25     5
6           26     5

Where all I want to extract is rows 3 and 4. I need to know where the id column is the same but exchange is different.

I think if I get this, I'll be able to build the other 2 tables into the query.

Thanks for the help!

SELECT * FROM my_table NATURAL JOIN (
  SELECT id FROM my_table GROUP BY id HAVING COUNT(DISTINCT exchange) > 1
) t

See it on sqlfiddle .

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