Finding duplicate values in MySQL

2018-12-31 10:03发布

I have a table with a varchar column, and I would like to find all the records that have duplicate values in this column. What is the best query I can use to find the duplicates?

标签: mysql
22条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 10:49
SELECT * 
FROM `dps` 
WHERE pid IN (SELECT pid FROM `dps` GROUP BY pid HAVING COUNT(pid)>1)
查看更多
路过你的时光
3楼-- · 2018-12-31 10:50
SELECT varchar_col
FROM table
GROUP BY varchar_col
HAVING count(*) > 1;
查看更多
君临天下
4楼-- · 2018-12-31 10:51

Assuming your table is named TableABC and the column which you want is Col and the primary key to T1 is Key.

SELECT a.Key, b.Key, a.Col 
FROM TableABC a, TableABC b
WHERE a.Col = b.Col 
AND a.Key <> b.Key

The advantage of this approach over the above answer is it gives the Key.

查看更多
旧时光的记忆
5楼-- · 2018-12-31 10:51

The following will find all product_id that are used more than once. You only get a single record for each product_id.

SELECT product_id FROM oc_product_reward GROUP BY product_id HAVING count( product_id ) >1

Code taken from : http://chandreshrana.blogspot.in/2014/12/find-duplicate-records-based-on-any.html

查看更多
牵手、夕阳
6楼-- · 2018-12-31 10:52

To find how many records are duplicates in name column in Employee, the query below is helpful;

Select name from employee group by name having count(*)>1;
查看更多
不流泪的眼
7楼-- · 2018-12-31 10:52
SELECT t.*,(select count(*) from city as tt
  where tt.name=t.name) as count
  FROM `city` as t
  where (
     select count(*) from city as tt
     where tt.name=t.name
  ) > 1 order by count desc

Replace city with your Table. Replace name with your field name

查看更多
登录 后发表回答