mySQL query to find duplicate row

一曲冷凌霜 提交于 2019-12-19 11:28:21

问题


Exmaple:

[empid    date      bookid]
----------
1        5/6/2004   8

2        5/6/2004   8

1        5/7/2004   8

1        5/8/2004   6

3        5/8/2004   8

2        5/8/2004   7

In this table,I need to get empid 1 as output..since it has bookid 8 more than once..

thanks in advance..


回答1:


You can use:

SELECT DISTINCT id
FROM table
GROUP BY empid, bookid
HAVING COUNT(*) > 1

But it will give you duplicates. If, for example, you have 1-8,1-8,1-9,1-9 you will get 1,1 as output because empid 1 has duplicate bookid's for two distinct bookid values. You will need to use SELECT DISTINCT to filter out the duplicate empid.




回答2:


SELECT empid
from table
group by empid
having Count(distinct bookid) > 1


来源:https://stackoverflow.com/questions/3862288/mysql-query-to-find-duplicate-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!