T -SQL用于从更新中选择

时间:2017-03-20 14:51:47

标签: sql sql-server tsql

我有三张桌子:

Customer: Id, FirstName, LastName, Email
CommunityUser: Id, FirstName, LastName, Email , CustomerId
BlogComment: Id, CustomerId, Text

如何使用T-SQL为每个BlogComment.CustomerId设置相应的CommunityUser.Id
我试过了:

For select * from  BlogComment bc
update bc set CustomerId = (select cu.Id from CommunityUser cu Where cu.CustomerId = bc.CustomerId)

但它不是有效的语法

3 个答案:

答案 0 :(得分:1)

加入CommunityUser的简单更新将有助于

Update B
set b.Customerid = cu.id
from BlogComment b inner join CommunityUser cu
on b.Costumerid = cu.CustomerId

答案 1 :(得分:0)

update BlogComment bc
set CustomerId = (select Id from CommunityUser cu Where cu.CustomerId = bc.CustomerId)

答案 2 :(得分:0)

试试

UPDATE bc 
SET bc.CustomerId = cu.Id 
FROM CommunityUser cu INNER JOIN BlogComment bc 
ON cu.CustomerId = bc.CustomerId
相关问题