Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find duplicate names in a table

I have a table having person names address and job title. the names are repeating sometimes. I have to compare if two people have the same name and same address than i have to keep only 1 record of them.

Table: Data_Excel
Name: P_Name
Address: P_Address
City:  P_city
like image 265
Mouzzam Hussain Avatar asked Nov 20 '13 10:11

Mouzzam Hussain


2 Answers

To find the duplicates you can do:

SELECT P_name,
  P_Address,
  P_city
FROM Data_Excel
GROUP BY P_Name,
  P_Address,
  P_city
HAVING COUNT(*) > 1;

To remove duplicates you could do:

DELETE
FROM Data_Excel
WHERE rowid NOT IN (
    SELECT MIN(rowid)
    FROM Data_Excel
    GROUP BY P_Name,
      P_Address,
      P_city
    );

To Insert in Person table you would do:

INSERT INTO Person(id,name)
SELECT (SELECT MAX(id)+1 FROM Person),P_Name 
FROM Data_Excel WHERE P_Name NOT IN (SELECT name FROM Person)
like image 140
Filipe Silva Avatar answered Oct 19 '22 20:10

Filipe Silva


SELECT P_Name,P_Address,count(*)
FROM Data_Excel
GROUP BY P_Name,P_Address
HAVING count(*) > 1;

This will give you the records with same P_Name & P_Address.

like image 32
SajjadHashmi Avatar answered Oct 19 '22 21:10

SajjadHashmi