0

I try to create mysql event that should delete duplicate rows of table.

This is my query

DELETE FROM locations
WHERE id NOT IN (SELECT  id
                 FROM locations
                 GROUP BY acc_id,`date`)

I got this error:

Error Code: 1093
You can't specify target table 'locations' for update in FROM clause.

How can I change the query to make it work?

1

2 Answers 2

3

In MySQL you can't delete from the same table you are selecting from. But you can trick MySQL with another subselect

DELETE FROM locations
WHERE id NOT IN 
(
  select * from 
  (
      SELECT id
      FROM locations
      GROUP BY acc_id, `date`
  ) x
)
Sign up to request clarification or add additional context in comments.

Comments

0

Try to provide the custom alias in your subquery you cannot directly specify the same table in update/delete

DELETE FROM locations
WHERE id NOT IN (
SELECT new_table.id FROM (
SELECT  id
FROM locations
GROUP BY acc_id,`date`
) new_table    
)

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.