Write a query to to delete duplicate rows?

Answer Posted / prakash.matte

If we have table 'temp' with columns: id(primary key),
field1, field2, field3, to remove the duplicate (field1,
field2, field3) data we can use either of the below queries

1. delete t1.* from temp t1, temp t2
where t1.id>t2.id and t1.field1=t2.field1 and
t1.field2=t2.field2 and t1.field3=t2.field3;

2. alter ignore table temp ADD UNIQUE INDEX
duprows(field1,field2,field3);

> here we can remove the duprows index by using the
following query:

alter table temp drop index duprows;

we can apply index on the columns as we don't require the
duplicate data, so we don't need to execute the query to
delete the index

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is php form validation?

732


Explain what is the difference between mysql_fetch_array() and mysql_fetch_assoc()?

642


Where do we use get and post?

700


How error handling is being handled by php?

741


What is the functionality of md5 function in php?

766


How do I find out the number of parameters passed into function9?

714


Is it possible to protect special characters in a query string?

743


What is multidimensional array in php?

729


What are the different opening and closing tags available in PHP?

762


What is boolean in php?

718


What is the use of strpos in php?

767


Why ide is recommended for use while programming with php?

679


What is the meaning of ‘escaping to php’?

804


Which function can be used to exit from the script after displaying the error message?

706


How are php sessions stored?

715