sql查询找到双重条目

时间:2017-05-04 20:19:17

标签: mysql sql

我在SQL查询中遇到一些问题,无法找到双重条目:

mytable的

onStop()

一些插页:

id(int), eid(int), date(date), pid(int)

我想找到一个日期的eid的双重条目 结果:

1, 10, '2017-05-04', 1  
2, 20, '2017-05-04', 1  
3, 10, '2017-05-04', 5

我的第一个想法是使用GROUP BY和HAVING,但它不起作用。

1, 10, '2017-05-04', 1  
3, 10, '2017-05-04', 5

如果你试图帮助我,那就结束了我的一天,谢谢你的进步!

2 个答案:

答案 0 :(得分:2)

使用exists()

select *
from mytable t
where exists (
  select 1
  from mytable i
  where t.eid = i.eid
    and t.date = i.date
    and t.id <> i.id
    )

inner join版本,但您可能会获得额外的行数,具体取决于每对的重复数量。如果是这样,您可以添加distinct

select t.*
from mytable t
  inner join mytable i
    on t.eid = i.eid
   and t.date = i.date
   and t.id <> i.id

答案 1 :(得分:0)

您应该尝试此解决方案:expr

相关问题