[英]How can I update a column in a table where I am using the same table to filter the data?
I am trying to update a column here.我想在这里更新一个专栏。 When I run select statement, it gives proper results.
当我运行 select 语句时,它给出了正确的结果。 But I am not able to update.
但我无法更新。 Here is the script that I am running....
这是我正在运行的脚本....
update table1
set col1 = 0
where col1 = 1 and col2 not in (select col3 from table1);
MySQL does not allow subqueries to reference the table being updated. MySQL 不允许子查询引用正在更新的表。 So, use a
left join
instead:因此,请改用
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;
I also strongly advise you not to use not in
with subqueries.我也强烈建议你不要使用
not in
与子查询。 The not in
doesn't usually do what you intend when any value in the subquery is NULL
.当子查询中的任何值为
NULL
时, not in
通常不会按照您的意图执行。 Hence, it is just a bad practice to use something that sometimes does not do what you intend.因此,使用有时不符合您的意图的东西只是一种不好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.