我尝试创建 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.
如何更改查询以使其正常工作?
答案 0 :(得分:3)
在MySQL中,您无法从您选择的同一个表中删除。但是你可以用另一个子选项来欺骗MySQL
DELETE FROM locations
WHERE id NOT IN
(
select * from
(
SELECT id
FROM locations
GROUP BY acc_id, `date`
) x
)
答案 1 :(得分:0)
尝试在子查询中提供自定义别名,不能在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
)