首页 > 解决方案 > 如何在 MySQL 中的另一列中共享相同值的表中返回重复项?

问题描述

我将如何返回相同年份的数字重复的 ProductNumbers ?这都在同一个表中。在下面的示例中,我希望返回 ProductNumber 123 和 456。如有可能请说明理由,谢谢!

ProductNumber   Numb     Year
123             45        1
456             45        1
789             45        2
109             54        2

标签: mysqlsql

解决方案


这是使用的一个选项exists

select * 
from yourtable t
where exists (
    select 1
    from yourtable t2
    where t.productnumber != t2.productnumber 
          and t.numb = t2.numb
          and t.year = t2.year
    )

使用exists,我们检查同一个表中是否有其他记录productnumber不同,但具有相同的numbyear值。


推荐阅读