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 are the php functions?

554


Describe the security vulnerability of PHP?

611


What is scope of variable in php?

578


What are the difference between echo and print?

536


In php, objects are they passed by value or by reference?

524






Explain what are psrs?

508


How to get ip address of clients machine?

561


Will a comparison of an integer 12 and a string "13" work in php?

543


How to get length of an array in PHP?

568


How to remove values saved in the current session?

537


What is the current stable version of php? What advance thing in php7?

516


Explain what is memcache?

510


Explain how we can retrieve the data in the result set of mysql using php?

502


What is the use of header() function in php?

549


What are the rules to determine the “truth” of any value which is not already of the boolean type?

516