Write a query to to delete duplicate rows?

Answer Posted / vipul dalwala

For emample we have table called DUPtable.

DESCRIBE DUPtable;

+--------+-----------------+
| Field | Type |
+--------+-----------------+
| id | Auto Increament |
| field1 | varchar(20) |
| field2 | varchar(20) |
| field3 | varchar(20) |
+--------+-----------------+

And we want to delete duplicate rows from DUPtable (Same
combination of field1, field2 and field3) .

Solution:

Step 1: Create a Temporary table;

CREATE TEMPORARY TABLE tmpDUPtable
SELECT id FROM DUPtable GROUP BY field1, field2, field3;


Step 2: Delete query to remove Rows not in 'tmpDUPtable'
table.

DELETE from DUPtable WHERE id NOT IN (SELECT id FROM
tmpDUPtable);

Step 3 DROP tmpDUPtable

DROP TABLE tmpDUPtable;


I hope this will help.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is preg_match?

540


Tell me what sized websites have you worked on in the past?

499


How to access a global variable inside a function?

532


CWD is a type of shell variable. State Whether True or False?

571


Do you know what does mvc stand for and what does each component do?

499






What is difference between static and constant?

530


What is different between software and app

1227


How to store the uploaded file to the final location?

514


Is it possible to extend the execution time of a php script?

491


What are the characteristics of php variables?

523


What is the Default syntax used in PHP?

594


What is memcache?

540


Where are php configuration settings stored?

529


What are php string functions?

525


Which Scripting Engine PHP uses?

556