-1

i having a problem in cleaning data from the entry in database.

so i have a table "users" with the field (id, name, email, phone)

the problem i have is to delete the duplication based on the phone number. i have around 30k of data entry and i need to make sure that each of the data in the table must consist a record with different "phone number" bcoz right now what i have is (example: 3 same people with the same phone number

name:   Phone No:
john     1234
john     1234
john     1234

i only need to keep one record with one phone number.

is there any php script than can work on this case faster.hope you guys can help me.

2 Answers2

0

You can delete with a join. In this example you keep the lowest users.id.

DELETE t1 FROM users AS t1
LEFT JOIN
(SELECT MIN(id) AS min_id FROM users GROUP BY phone) AS t2
ON t1.id = t2.min_id WHERE t2.min_id IS NULL;
Max Oriola
  • 1,296
  • 12
  • 8
0

You can use ALTER TABLE

ALTER IGNORE TABLE users
ADD UNIQUE INDEX p (phone);

This will drop all the duplicate rows and doesn't allow future INSERT with the same phone value.

Aso Strife
  • 1,089
  • 3
  • 12
  • 31