添加来自不同表的值

时间:2012-11-11 04:45:21

标签: sql-server-2005

朋友们,我有这些表格:

Contestant Table:
Winner
Peter

Group Table:
Id Name Score Union
1 Bryan 3 77
2 Mary 1 20
3 Peter 5 77
4 Joseph 2 25
5 John 6 77

我希望在小组桌上给Peter增加5分。所以,我提出了这个问题。

UPDATE Group 
SET Score = Score+5
FROM Contestant, Group 
WHERE Contestant.Winner = Group.Name

现在,我还想给同一个联盟提供5分的额外分数,比如77.

如何将其作为一个查询集成到现有查询中?

// UPDATE 1

我有类似的问题,但使用3个表:

1. Company (table)
CONAME
COPOINTS

2. Group_Member (table)
CONAME
NAME

3. Member (table)
NAME
MPOINTS

我想通过以下条件得到正确的查询:

Update Member
Set MPOINTS=MPOINTS+5
Where Company.CONAME=Group_Member.CONAME
And
Group_Member.NAME=Member.NAME

请您更正以上查询?

2 个答案:

答案 0 :(得分:0)

update u set score = score + 5
from contestant c
inner join group g on g.name = c.winner
inner join group u on u.union = g.union

目前尚不清楚,但如果您希望获胜者再获得5分:

update u set score = (case
  when u.name = c.winner then score + 10
  else then score + 5 end)
from contestant c
inner join group g on g.name = c.winner
inner join group u on u.union = g.union

答案 1 :(得分:0)

UPDATE Group 
SET Score = Score+5
WHERE [Union] = (
    SELECT [Union] 
    FROM Group, Contestant 
    WHERE Group.Name = Contestant.Winner)