UPDATE Users
SET GeneralPoint =(SELECT PostResult.AveragePoint FROM Users
LEFT JOIN (SELECT AVG(PostsLikes.PostPoint) AS AveragePoint , PostsLikes.ID_User
FROM PostsLikes,Users
WHERE PostsLikes.ID_User = Users.Id
GROUP BY PostsLikes.ID_User) PostResult
ON Users.Id = PostResult.ID_User)
答案 0 :(得分:0)
您可以使用相关子查询:
UPDATE u
SET u.GeneralPoint = (SELECT AVG(pl.PostPoint) FROM PostsLikes pl WHERE pl.ID_User = u.id )
FROM Users u;
答案 1 :(得分:0)
--This should update all the user columns with the subquery values
--This works as it joins the Users table on your subquery by Users.Id
--I added the Users.Id value to the initial sub-query so you can join on it for the update
UPDATE Users
SET GeneralPoint = x.AveragePoint
FROM Users AS u
INNER JOIN
(SELECT PostResult.AveragePoint, Users.Id FROM Users
LEFT JOIN (SELECT AVG(PostsLikes.PostPoint) AS AveragePoint , PostsLikes.ID_User
FROM PostsLikes,Users
WHERE PostsLikes.ID_User = Users.Id
GROUP BY PostsLikes.ID_User) PostResult
ON Users.Id = PostResult.ID_User) AS x
ON x.Id = u.Id
使用Select语句引用更新:(How do I UPDATE from a SELECT in SQL Server?)
答案 2 :(得分:0)
使用加入
UPDATE u
set GeneralPoint =b.avg_val
from Users u join
(SELECT AVG(pl.PostPoint) avg_val,pl.ID_User
FROM PostsLikes pl group by pl.ID_User
) b on u.Id=b.ID_User