0

我尝试创建应该删除重复的表行的mysql事件。

这是我的查询

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

我收到了这个错误:

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

如何更改查询以使其正常工作?

4

2 回答 2

3

在 MySQL 中,您不能从您选择的同一个表中删除。但是你可以用另一个子选择来欺骗 MySQL

DELETE FROM locations
WHERE id NOT IN 
(
  select * from 
  (
      SELECT id
      FROM locations
      GROUP BY acc_id, `date`
  ) x
)
于 2013-10-03T06:44:31.213 回答
0

尝试在您的子查询中提供自定义别名,您不能直接在更新/删除中指定同一个表

DELETE FROM locations
WHERE id NOT IN (
SELECT new_table.id FROM (
SELECT  id
FROM locations
GROUP BY acc_id,`date`
) new_table    
)
于 2013-10-03T06:44:56.333 回答