Reputation: 5148
Consider we have a field named (username) in our table named (tpl_users), now this table has lots of duplicated rows
I wrote this code to delete duplicated usernames:
Delete FROM tpl_users WHERE username = username;
How is it possible to delete duplicated usernames?
Upvotes: 1
Views: 615
Reputation: 2779
If you want to keep the users with the lowest id, first make sure this query has what you want to remove (and backup your database):
SELECT u1.id, u1.username FROM tpl_users u1 LEFT JOIN tpl_users u2 ON u1.username = u2.username WHERE u1.id > u2.id;
Then, if your database is backed up, and you're sure the above statement represents what you want to remove.
DELETE u1 FROM tpl_users u1 LEFT JOIN tpl_users u2 ON u1.username = u2.username WHERE u1.id > u2.id
Upvotes: 1
Reputation: 589
Assuming you have a primary key column in the table, you can do this:
DELETE
FROM tpl_uers
USING tpl_users, tpl_users AS vtable
WHERE vtable.id > tpl_users.id
AND tpl_users.username = vtable.username
Upvotes: 0
Reputation: 35141
What you want to do is delete duplicate rows.
You'll do this by finding all non-duplicate rows, and replacing the table with those rows:
create table tpl_users_new as select distinct * from tpl_users;
alter table tpl_users rename to tpl_users_old;
alter table tpl_users_new rename to tpl_users;
Upvotes: 1
Reputation: 753615
Your query deletes all the rows where the user name is not NULL.
If you want to identify the user names which are associated with more than one row:
SELECT username
FROM tpl_users
GROUP BY username
HAVING COUNT(*) > 1;
Before converting that into a DELETE, you need to be aware that the majority of queries will delete all the records associated with a duplicated name, rather than retaining just one of the duplicates.
Upvotes: 2