tags:

views:

61

answers:

4

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?

+1  A: 

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.

Jonathan Leffler
but i should delete them manually , and its a little bit hard if i have more than 100 duplicated username
Mac Taylor
+1  A: 

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;
tpdi
If there are entries with duplicate user names but, say, different addresses, then you can still end up with multiple rows for a single name. The basic technique - copy data into a new table and go from there - is correct, but the selection criteria may need to be more complex. (It is, of course, far better to prevent the problem from occurring, but it is also too late to cry over the spilt milk.)
Jonathan Leffler
Yes, you're right, but if the OP has rows that have the username but different data in other columns, he has a larger problem and thus a different question to ask. My advice prevents him form, e.g, deleting a key that's referenced by another table. Given his knowledge of SQL, I think this is the safest advice to give; he can ask follow-up questions if this doesn't solve his larger problem.
tpdi
A: 

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
Art Peterson
A: 

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
AlReece45