mySQL query to find duplicate row

2019-02-21 02:00发布

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..

2条回答
一夜七次
2楼-- · 2019-02-21 02:15
SELECT empid
from table
group by empid
having Count(distinct bookid) > 1
查看更多
叛逆
3楼-- · 2019-02-21 02:36

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.

查看更多
登录 后发表回答