mysql - 如何在使用同一个表过滤数据的表中更新列?
问题描述
我正在尝试在这里更新一列。当我运行 select 语句时,它会给出正确的结果。但我无法更新。这是我正在运行的脚本....
update table1
set col1 = 0
where col1 = 1 and col2 not in (select col3 from table1);
解决方案
MySQL 不允许子查询引用正在更新的表。所以,使用 aleft 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
子查询。当子查询not in
中的任何值为NULL
. 因此,使用有时不符合您预期的东西只是一种不好的做法。
推荐阅读
- r - 为什么误差条放置在图(组)的中心?
- operating-system - 使用信号量同步的调度过程
- r - Invert the order of the legend
- c# - How to properly implement ILogger.IsEnabled() in custom logger in ASP.NET Core MVC
- python - I need python to to check if variable is an integer using the input() command
- node.js - 了解 Firebase 如何处理使用 JWT 登录
- c++ - 声明变量的分段错误 C++
- go - 不能在 syscall.SetsockoptInt 的参数中使用 int(fd)(int 类型的值)作为 syscall.Handle 值
- python - 如何在指定时间内在 django 中运行函数?
- openlayers - 在OL6中捕捉绘图的结尾