14

我正在尝试使用另一个表的总和对表进行简单的更新,但由于某种原因,它只更新了一行。以下是表格中的相关信息:

游戏

gameplayer|points
----------------
John      |5
Jim       |3
John      |3
Jim       |4

球员生涯

playercareername|playercareerpoints
-----------------------------------
John            |0
Jim             |0

现在最终,我希望最后一个表在运行更新后看起来像这样:

球员生涯

playercareername|playercareerpoints
-----------------------------------
John            |8
Jim             |7

这是我尝试的仅更新第一行的查询:

UPDATE playercareer
SET playercareer.playercareerpoints = 
    (
SELECT 
    SUM(games.points) 
FROM games
    WHERE
     playercareer.playercareername=games.gameplayer
    )

我似乎找不到这个问题的答案。提前感谢您的时间和建议!

4

2 回答 2

43
UPDATE playercareer c
INNER JOIN (
  SELECT gameplayer, SUM(points) as total
  FROM games
  GROUP BY gameplayer
) x ON c.playercareername = x.gameplayer
SET c.playercareerpoints = x.total
于 2012-04-10T20:08:07.797 回答
0

表名不同,但这对我有用

update e
set e.Absence_Hours = a.hours
from [Thomas_Test].[dbo].Employee e
inner join (select empnum
                 , sum(hours) as hours
            FROM [Thomas_Test].[dbo].[Employee_Absence_Report]
            group by empnum) a 
        on a.empnum = e.emp_no;
于 2022-01-12T18:26:43.670 回答