Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting duplicate record from table - SQL query

I need to delete duplicate rows only from the table, like I have 3 duplicate rows in the table, my query will delete 2 rows from 3 duplicated rows.

How can I get this? Please help me.

like image 714
Santanu Avatar asked Nov 17 '09 12:11

Santanu


1 Answers

Please try the below query, it will definitely meet your objective

SET ROWCOUNT 1
DELETE test
FROM test a
WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1
WHILE @@rowcount > 0
  DELETE test
  FROM test a
  WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1
SET ROWCOUNT 0

where test is your table name

like image 159
Muhammad Akhtar Avatar answered Sep 30 '22 15:09

Muhammad Akhtar