mysql:[Err] 1093 - You can‘t specify target table ‘表名‘ for update in FROM clause问题

原始sql

delete from at_phone where id not in(select id from at_phone where id!=1)

运行这段sql语句,结果报错
在这里插入图片描述

[Err] 1093 - 您不能在FROM子句中指定目标表“ at_phone”进行更新

原因解决:如果在增删改语句中,要使用子查询的形式进行增删改,那么应该把这个子查询进行第二次select一下并且给上表别名,才可以执行。这个第二次select实际上就是把第一次的select的结果集放在临时表中。

解决后的sql:

delete from at_phone where id not in(select * from (select id from at_phone where id!=1) a)

在这里插入图片描述
执行成功

猜你喜欢

转载自blog.csdn.net/weixin_42825651/article/details/108618796
今日推荐