Last active
August 29, 2015 14:01
-
-
Save thewinterwind/862b235c3c0aad3532dc to your computer and use it in GitHub Desktop.
How to remove duplicate rows from a MySQL table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- For example, a members table that has duplicate emails | |
-- Needs unique id field to work | |
create temporary table tmpTable (id int); | |
insert tmpTable | |
(id) | |
select id | |
from members m | |
where exists | |
( | |
select * | |
from members m2 | |
where m2.email = m.email | |
and m2.id > m.id | |
); | |
delete | |
from members | |
where ID in (select id from tmpTable); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment