我想更新一个表,其中字段的值等于选择语句的结果。我的表如下:
Type Total#
A 4
B 8
C 1
我希望根据一个select语句的结果来更新上述表格。 以下是我的代码:
update MainTable
set [Total#] =
(SELECT count(distinct r.[ID])as Type
FROM dbo.TableA r left join
dbo.TableB a
on r.Post_ID = a.Post_ID
where a.Status is null)
如果我按原样运行代码,它将更新所有行,但我只想更新选择语句中的 Type 等于 MainTable 中的 Type 的行。谢谢。