Copy USE testdb;
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR ( 50 ) DEFAULT NULL ,
last_name VARCHAR ( 50 ) DEFAULT NULL ,
email VARCHAR ( 255 ) NOT NULL
);
INSERT INTO contacts (first_name,last_name,email)
VALUES ( 'Carine ' , 'Schmitt' , 'carine.schmitt@yiibai.com' ),
( 'Jean' , 'King' , 'jean.king@gmail.com' ),
( 'Peter' , 'Ferguson' , 'peter.ferguson@google.com' ),
( 'Janine ' , 'Labrune' , 'janine.labrune@qq.com' ),
( 'Jonas ' , 'Bergulfsen' , 'jonas.bergulfsen@mac.com' ),
( 'Janine ' , 'Labrune' , 'janine.labrune@qq.com' ),
( 'Susan' , 'Nelson' , 'susan.nelson@qq.com' ),
( 'Zbyszek ' , 'Piestrzeniewicz' , 'zbyszek.piestrzeniewicz@att.com' ),
( 'Roland' , 'Keitel' , 'roland.keitel@yahoo.com' ),
( 'Julie' , 'Murphy' , 'julie.murphy@yahoo.com' ),
( 'Kwai' , 'Lee' , 'kwai.lee@google.com' ),
( 'Jean' , 'King' , 'jean.king@qq.com' ),
( 'Susan' , 'Nelson' , 'susan.nelson@qq.com' ),
( 'Roland' , 'Keitel' , 'roland.keitel@yahoo.com' );
Copy SELECT
email, COUNT (email)
FROM
contacts
GROUP BY email
HAVING COUNT (email) > 1 ;
Copy + -------------------------+--------------+
| email | COUNT (email) |
+ -------------------------+--------------+
| janine.labrune@qq.com | 2 |
| roland.keitel@yahoo.com | 2 |
| susan.nelson@qq.com | 2 |
+ -------------------------+--------------+
3 rows in set
Copy DELETE t1 FROM contacts t1
INNER JOIN
contacts t2
WHERE
t1.id < t2.id AND t1.email = t2.email;
Query OK, 3 rows affected
Copy SELECT
email, COUNT (email)
FROM
contacts
GROUP BY email
HAVING COUNT (email) > 1 ;
Copy DELETE t1 FROM contacts t1
INNER JOIN
contacts t2
WHERE
t1.id > t2.id AND t1.email = t2.email;
Copy CREATE TABLE source_copy FROM source;
Copy INSERT INTO source_copy
SELECT * FROM source
GROUP BY col; -- column that has duplicate values
Copy DROP TABLE source;
ALTER TABLE source_copy RENAME TO source;
Copy -- step 1
CREATE TABLE contacts_temp
LIKE contacts;
-- step 2
INSERT INTO contacts_temp(email) SELECT email FROM contacts GROUP BY email;
-- step 3
DROP TABLE contacts;
ALTER TABLE contacts_temp
RENAME TO contacts;