0

In my database there is a table which consist redundant data. It means it is having the same data multiple times in multiple rows.

I want to delete all redundant data and keep only one record. The problem is my table has no primary or unique key now. But I have data in it.

Could you give me an example of a MySQL query that delete all duplicate row ?

DELETE 
FROM enr_money_handover_detail 
WHERE lastupdate IN (
                      SELECT lastupdate 
                      FROM enr_money_handover_detail 
                      GROUP BY lastupdate); 

This shows the error like

You can't specify target table 'enr_money_handover_detail' for update in FROM clause".

5
  • 1
    show your table structure, sample data, and expected result Commented May 7, 2015 at 13:22
  • DELETE FROM enr_money_handover_detail WHERE lastupdate IN (SELECT lastupdate FROM enr_money_handover_detail GROUP BY lastupdate); this shows the error like "You can't specify target table 'enr_money_handover_detail' for update in FROM clause". Commented May 7, 2015 at 14:07
  • :- instead of put code in comment, edit your question. This time I will edit your question to add code. Commented May 7, 2015 at 14:09
  • Still struggling? If you like, consider following this simple two-step course of action: 1. If you have not already done so, provide proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem. 2. If you have not already done so, provide a desired result set that corresponds with the information provided in step 1. Commented May 7, 2015 at 14:15
  • I m new so struggling a bit. Commented May 7, 2015 at 14:24

3 Answers 3

2

SELECT DISTINCT is you friend. Create a new table and insert into from a query using select distinct from your old table.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you need to take copy of all distinct records from your current table to some other table (backup).

And then truncate your original table and then copy back the records from your backup table to original table.

Below commands will help you to achieve it.

CREATE TABLE handover_backup SELECT DISTINCT * FROM enr_money_handover_detail;

TRUNCATE TABLE enr_money_handover_detail;

INSERT INTO enr_money_handover_detail SELECT * FROM handover_backup; 

Hope this will help you.

2 Comments

I think back up table should have all data instead of DISTINCT data, When you transfering data from Backup table to Original table then use Distinct... Other wise there is an change to lost data.
We can do either way, the ultimate goal is to delete duplicate entries and both will solve the purpose.
-1

As someone else stated you can use Select Distinct, and use a simple Delete query this way:

DELETE FROM enr_money_handover_detail WHERE lastupdate IN (SELECT DISTINCT(lastupdate) FROM enr_money_handover_detail); 

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.