选择和删除
我需要从表中删除某些行。哪些行必须被删除是我通过查询找到的。但是,看来您无法在相同的查询:
目前,您无法从 表并从同一个表中选择 在子查询中。
所以我不能这样做:
DELETE
FROM telefono
WHERE telefono_id IN (
SELECT te.telefono_id
FROM telefono te
LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)
);
-- SQL Error (1093): You can't specify target table for update in FROM clause
如何在纯MySQL中实现这种记录清理?
服务器运行MySQL 5.1.39。
I need to remove certain rows from a table. Which rows must be deleted is something I find out through a query. However, it appears that you cannot do both operations (select and delete) in the same query:
Currently, you cannot delete from a
table and select from the same table
in a subquery.
So I cannot do this:
DELETE
FROM telefono
WHERE telefono_id IN (
SELECT te.telefono_id
FROM telefono te
LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)
);
-- SQL Error (1093): You can't specify target table for update in FROM clause
How can I implement this record clean-up in pure MySQL?
The server runs MySQL 5.1.39.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用连接执行删除语句
Try doing the delete statement with the joins
使用
UPDATE
标记要删除的行,然后使用DELETE
实际删除它们。Use
UPDATE
to mark rows for deletion, thenDELETE
to actually delete them.