查询无法按预期工作

时间:2012-11-25 14:23:57

标签: sql database oracle procedure

我们有这些表格:

player(id,salary,bonus)

productivity_per_year(id,goals,year)
  

生产力表(ID,目标,年份)

+----+-------+------+
| ID | Goals | Year |
+----+-------+------+
| 01 |    20 | 2001 |
| 01 |    30 | 2002 |
| 02 |    30 | 2001 |
| 02 |    40 | 2002 |
+----+-------+------+

问题是,如果玩家过去两年(2001年和2002年)达到30个或更多目标,我怎样才能提高玩家的奖金。例如,我希望奖金由id = 02的玩家拍摄,因为他在2001年和2002年都得分> = 30.

我正在使用以下程序,但它失败了,因为它增加了两个玩家的奖金!!!

create or replace 
procedure football
AS
BEGIN
   update player p
      set p.bonus = p.bonus + 500
   where p.id in 
         (select s.id from productivity s where
            s.goals >30 
        and  s.year>=2001
        and s.id = p.id
         );
END;

1 个答案:

答案 0 :(得分:1)

两名球员都获得了更大的奖金,因为自2001年以来,这两名球员都打进了超过30个进球。这就是你对你的查询所做的。但是,要仅在2001年和2002年获得超过30个进球的球员获得奖金,您可以尝试以下查询。

update player p 
set bonus = bonus + 500 
where p.id in 
((select s.id from productivity s where s.goals > 30 and s.year = 2001 and s.id = p.id)
intersect
(select s.id from productivity s where s.goals > 30 and s.year = 2002 and s.id = p.id));

使用postgresql可以正常工作。

相关问题