我需要从表中删除某些行。必须删除哪些行是我通过查询发现的。然而,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.
所以我不能这样做:
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。
最佳答案
尝试使用连接执行删除语句
DELETE te
FROM telefono as 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)
关于mysql - 选择和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3606451/