SQL Server - ID 在 SELECT 中的更新表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6868291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:31:01  来源:igfitidea点击:

SQL Server - UPDATE table where ID is in SELECT?

sqlsql-serversql-update

提问by dpp

I have a table where I want to update all rows with the ID that exists in the select result.

我有一个表,我想用选择结果中存在的 ID 更新所有行。

My pseudo-code:

我的伪代码:

UPDATE mytable as t
   SET t.status = 'PM'
 WHERE t.ID EXISTS IN (select ID from ...)

I have managed to do the select statement, now I want to use the result of the select statement to update a table.

我已经设法执行了 select 语句,现在我想使用 select 语句的结果来更新表。

回答by Jesus Ramos

If you remove the exists you have a valid query from what I can tell.

如果你删除存在,你有一个有效的查询,我可以告诉你。

UPDATE mytable 
   SET status = 'PM'
 WHERE id IN (select ID from ...)

Works for me in MySql 5.5, not sure which database you're using.

在 MySql 5.5 中对我有用,不确定您使用的是哪个数据库。

回答by user3388611

One cannot use substitution in the UPDATE statement. The original query should be good when you leave out the " as t" part and both "t.".

不能在 UPDATE 语句中使用替换。当您省略“as t”部分和“t.”部分时,原始查询应该是好的。