Mysql 在使用同一个表过滤数据的表中,如何更新列?

Mysql 在使用同一个表过滤数据的表中,如何更新列?,mysql,sql,Mysql,Sql,我正在尝试更新这里的一个专栏。当我运行select语句时,它会给出正确的结果。但我无法更新。这是我正在运行的脚本 update table1 set col1 = 0 where col1 = 1 and col2 not in (select col3 from table1); MySQL不允许子查询引用正在更新的表。因此,请改用左连接: update table1 t1 left join table1 tt1 on t1.col2 = tt1.col3

我正在尝试更新这里的一个专栏。当我运行select语句时,它会给出正确的结果。但我无法更新。这是我正在运行的脚本

update table1
  set col1 = 0
where col1 = 1 and col2 not in (select col3 from table1);

MySQL不允许子查询引用正在更新的表。因此,请改用
左连接

update table1 t1 left join
       table1 tt1
       on t1.col2 = tt1.col3
    set col1 = 0
where t1.col1 = 1 and tt1.col3 is null;

我还强烈建议您不要在子查询中使用
not in
。当子查询中的任何值为
NULL
时,
不在
中通常不会执行您想要的操作。因此,使用某些有时无法达到预期效果的内容是一种不好的做法。

使用多表更新语法。@Akina。非常感谢。