繁体   English   中英

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

[英]How can I update a column in a table where I am using the same table to filter the data?

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

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

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

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时, not in通常不会按照您的意图执行。 因此,使用有时不符合您的意图的东西只是一种不好的做法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM