You can't specify target table 'tbl_students' for update in FROM clause错误

此问题只出现在mysql中 oracle中无此问题

在同一语句中,当你在select某表的数据后,不能update这个表,如:

DELETE FROM tbl_students WHERE id NOT IN (SELECT id FROM tbl_students GROUP BY `name`)

改成下面这样

DELETE FROM tbl_students WHERE id NOT IN (
SELECT t.id FROM
(SELECT id FROM tbl_students GROUP BY `name`)AS t)

将select出的结果再通过中间表select一遍就可以了

原文地址:https://www.cnblogs.com/Noctis/p/11104782.html