我有3个表格,结构如下:
表用户
表组
表用户组(关联表)
我正在尝试计算group_score - 只需通过添加组内用户的所有点 - 为所有组计算。
我尝试了以下查询
UPDATE `Group` SET group_score = (SELECT SUM(User.points) FROM User
JOIN UsersGroups
ON User.id = UsersGroups.UserId
GROUP BY UsersGroups.GroupId)
它给了我OperationalError('Subquery返回超过1行')
我做错了什么?
感谢您的帮助!
答案 0 :(得分:0)
UPDATE `Group` JOIN (SELECT GroupId, SUM(User.points) as sumuser FROM User
JOIN UsersGroups
ON User.id = UsersGroups.UserId
GROUP BY UsersGroups.GroupId) x
ON `Group`.Id=x.GroupId
SET group_score =x.sumuser
答案 1 :(得分:0)
试试这个
UPDATE `Group` SET group_score = (SELECT UsersGroups.GroupId SUM(User.points) FROM User
JOIN UsersGroups
ON User.id = UsersGroups.UserId
GROUP BY UsersGroups.GroupId)