简体   繁体   English

MySQL在其他行中选择具有相同FK但值不同的行

[英]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. 我要提取的只是第3行和第4行。我需要知道id列的位置是相同的,但是交换是不同的。

I think if I get this, I'll be able to build the other 2 tables into the query. 我想如果我得到这个,我将能够在查询中构建其他2个表。

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 . sqlfiddle上看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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