Having the following table
mysql> desc customer;
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| id | char(36) | NO | PRI | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| date_entered | datetime | YES | | NULL | |
| date_modified | datetime | YES | | NULL | |
| modified_user_id | char(36) | YES | | NULL | |
| created_by | char(36) | YES | | NULL | |
| description | text | YES | | NULL | |
| deleted | tinyint(1) | YES | | 0 | |
| assigned_user_id | char(36) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| pc | varchar(255) | YES | | NULL | |
| country | varchar(100) | YES | | NULL | |
| telephone | varchar(255) | YES | | NULL | |
| dob | date | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+
And a set of duplicate records similar to the following
mysql> SELECT email, COUNT(*) count FROM customer GROUP BY email HAVING count > 1 ORDER BY count DESC limit 10;
+-----------------------------+-------+
| email | count |
+-----------------------------+-------+
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
| [email protected] | 2 |
+-----------------------------+-------+
For example
mysql> select firstname, lastname, pc, country, telephone, dob, date_entered, deleted from customer where email = "[email protected]"
+-----------------------+----------+------------+---------+----------+-----------+----------+------+---------+
| firstname | lastname | pc | country | telephone | dob | date_entered | deleted |
+-----------------------+----------+------------+---------+-----------+------+---------------------+---------+
| ANTONIO CHICA GALLEGO | | | ES | NULL | NULL | 2023-03-24 21:50:15 | 0 |
| ANTONIO CHICA GALLEGO | | | ES | | NULL | 2023-04-04 09:42:11 | 0 |
+-----------------------+----------+------------+---------+-----------+-----------+----------+------+--------+
I need to remove one of these duplicate records, so that only one remains of each duplicate record
How to remove one of the duplicate records?
Thanks in advance
2
Answers
"[email protected]"
except latest date_enterededit:
WHERE
specifies (the records selected for deletion are those with the same email address + older date_entered) — selecting c1 inDELETE
removes the older recordYou could try to use this query
That means per each record, if there are existed other records that have the same
email
and smallerid
, then update value of itsdeleted
column to1
.This query will update value of
deleted
column of all records that have the sameemail
, except the one will smallestid
.In case that you actually want to delete records, just replace
UPDATE
withDELETE
statement.