1

I have a lot of telephone numbers that are duplicated in the telephone field. How can it be done by SQL?

I want to remove record that are duplicate.

I also want to know how many that are duplicated?

Thanks

I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • possible duplicate of http://stackoverflow.com/q/8567007/27535 or http://stackoverflow.com/q/8590010/27535 and many more – gbn Jan 31 '12 at 16:14
  • Must you use only SQL? Cant you write a bit code for that? It will be much easier with a code fuction – Bhrugesh Patel Jan 31 '12 at 16:16
  • possible duplicate of [remove duplicates in mysql database](http://stackoverflow.com/questions/8793231/remove-duplicates-in-mysql-database) – aF. Jan 31 '12 at 16:28

3 Answers3

1

Try this:

DELETE FROM phonenumbers WHERE telephone = "[phone number here]" AND id NOT IN (SELECT id FROM phonenumbers WHERE telephone = "[phone number here]" LIMIT 1)

This will remove all entries with that phone number, except the first one

Note, this is assuming you have a unique identifier ID in your table. (and your tablename is phonenumbers. Change that into your real tablename

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
0

This query might help:

DELETE `P`.*
FROM `phones` `P` 
LEFT JOIN ( 
    SELECT `telephone`, MIN(`id`) `ids` 
    FROM `phones` 
    GROUP BY `telephone` 
) `TA` ON `P`.`id` = `TA`.`ids` 
WHERE `TA`.`ids` IS NULL;

Please note to change the table names and field names as per your schema. Also, the above assumes that your table has a primary column, denoted as id in the above query.

The logic is:

  1. using the subquery, we first find out all telephone numbers and the first record for each number. These are the records that will remain and the rest deleted
  2. then we do a left join between "phones" table and the derived table, and delete all records from "phones" that do not match in the derived table

The benefit with the above query is that it will delete all duplicate records in one shot.

For the duplicate counts, you may do something like:

SELECT `telephone`, COUNT(1) `cnt` 
FROM `phones` 
GROUP BY `telephone` 
HAVING COUNT(1) > 1 

Hope it helps!

Abhay
  • 6,545
  • 2
  • 22
  • 17
0

Here's a simple one that copies your table to a new one lacking duplicate 'telephone' fields:

CREATE TABLE addrbook2
  SELECT * FROM addrbook GROUP BY telephone

You could then delete the old addrbook table, and rename the new addrbook2 to addrbook if you wanted.

gcbenison
  • 11,723
  • 4
  • 44
  • 82