![](/img/trans.png)
[英]Nested SQL Query where id from one table matches to another table where two specific entries exist
[英]SQL update from one Table to another where ID matches
我有 2 个数据库 Alpha 和 Beta。 每个数据库都有相同的表“评论”。 Comments 表有 2 个字段:ID 和 Comments。 Alpha.Comments 仅填充了 ID 字段,相应的 Comment 字段为空。 Beta.Comments 有 ID 和相应的评论。 是否有 SQL 查询可以使用 ID 匹配的 Beta 中的适当条目更新所有 Alpha.Comments 字段?
就像是:
update alpha.comments
set comments = (select b.comments from beta.comments b where b.id = comments.id);
在 SQL Server 中,可以表示为:
update a
set comments = b.comments
from alpha.comments a join
beta.comments b
on a.id = b.id;
这以一种不幸的方式处理重复项。 它任意选择一个进行赋值,因此不会产生错误。
已经有与此请求类似的帖子 - 请查看:
我确定还有其他的,所以你可以找到一个与你正在使用的数据库相关的。
你可以这样更新
UPDATE Alpha SET Comments=(
SELECT Comments
FROM Beta
WHERE Beta.ID = Alpha.ID
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.