0

I have a table name as bursary,

there is duplicate entries in the username, how can I delete the duplicate entries.?

here is my bursary table,

UserID    UserName
  1       ca11074
  2       ca11074
  3       cb56456
  4       cb56456

I need to delete duplicate entries.

here I tried but unfortunately not working.

$sql = 
"DELETE FROM bursary  
WHERE UserName IN (SELECT UserName FROM bursary (COUNT(*) > 1)";
$result=mysql_query($sql);
3
  • 1
    Which one do you want to keep? Also, as attempts go, this is fairly pathetic :-( Commented Dec 1, 2014 at 10:05
  • What does 'is not working' mean? What error messages do you get after applying appropriate error checking techniques? Commented Dec 1, 2014 at 10:06
  • I just want to keep the Username without any duplicates. Delete if the UserName exists same Commented Dec 1, 2014 at 10:13

7 Answers 7

1

Try this :

$sql = "ALTER IGNORE TABLE `bursary` ADD UNIQUE INDEX index_name (`UserName`);";
$result = mysql_query($sql);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use INNER JOIN.

DELETE a
FROM YOUR_TABLE AS a 
INNER JOIN YOUR_TABLE AS b ON a.UserName = b.UserName 
WHERE  a.UserID < b.UserID;

Or you can create another TEMP_TABLE, select distinct values from YOUR_TABLE to TEMP_TABLE, after it truncate YOUR_TABLE and copy TEMP_TABLE data to YOUR_TABLE and delete the TEMP_TABLE.

Comments

0

This will delete duplicates. It'll keep the row with the lowest ID.

DELETE FROM bursary a
WHERE exists (select 1 from bursary b
               where a.UserName = b.UserName and
                     a.id > b.id)

Comments

0

You can apply unique key to your Username column will delete duplicate entries.

Comments

0

You can try below query:

Delete bs from bursary bs,bursary bs2 
where bs.UserID > bs2.UserID and bs.UserName = bs2.UserName;

Using above query you can delete duplicate username entries.

Comments

0
delete from bursary b1 
where rowid > (SELECT min(rowid) FROM bursary b2 group by 
               b2.UserID,b2.UserName);

Comments

-1

please use add unique with ignore to delete all duplicate value

ALTER IGNORE TABLE `my_table`
    ADD UNIQUE (`Username`);

After that you can remove unique if you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.