0

I have used a mysql query to select my duplicate entries in Database.

select id,DATE_FORMAT(cu_date,'%Y-%m-%d %T'),trunk_id ,count(*) from circuit_u
WHERE DATE_FORMAT(cu_date,'%Y-%m-%d') = '2013-01-26'
group by DATE_FORMAT(cu_date,'%Y-%m-%d %T'),trunk_id
HAVING count(*) > 1;

Output:

16347   2013-01-26 01:00:00 0   2
16372   2013-01-26 01:00:00 1   2
16397   2013-01-26 01:00:00 100 2
16422   2013-01-26 01:00:00 101 2
16447   2013-01-26 01:00:00 121 2
16472   2013-01-26 01:00:00 211 2
16497   2013-01-26 01:00:00 221 2
16522   2013-01-26 01:00:00 311 2

Now I want to add another command to delete them.

I have tried with so many way but every time i failed.

Please help.

8
  • if you have tried so many why dont you post them ? so we can correct you not give you the coocked answer Commented Aug 4, 2013 at 9:35
  • Do you want to delete only the duplicates or every entry that has a duplicate? Commented Aug 4, 2013 at 9:48
  • every entry that has a duplicate. i mean whatever my above query returns will be deleted. because i am able to find the duplicate data only Commented Aug 4, 2013 at 9:49
  • The query you posted should not work, because you specify different columns in your projection (select) than in your grouping (group by). Does that query really work? Commented Aug 4, 2013 at 9:54
  • 1
    Your query is still wrong. You group by columns and then specify different columns in your select. How should you be able to get the ID (as first column), after you have already grouped your data by date and another id? This does not work! Commented Aug 4, 2013 at 10:08

2 Answers 2

3

you can try this

   DELETE from circuit_u WHERE id IN ( select * from (
   select id from circuit_u
   WHERE DATE_FORMAT(cu_date,'%Y-%m-%d') = '2013-01-26'
   group by DATE_FORMAT(cu_date,'%Y-%m-%d %T'),trunk_id
   HAVING count(*) > 1 ) t
   )
Sign up to request clarification or add additional context in comments.

2 Comments

sorry man no use. error 1241 operand should contain one column
This would delete all duplicate entries along, dosen't the OP wants to keep at-least 1 of the rows that contains duplicated value(s)
1

You can't access and delete rows from the same table in the same SQL query, no matter how you try, trust me. You'll need at least two queries.

I've done this before with three: "a query to create a temporary memory table for the IDs; another one to populate it with the row IDs you want to delete (or keep..); a last query which relates the original and the temporary table to delete rows from the first one."

(Note: If you use temporary tables, you'll have to execute the three queries in the same connection.)

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.