我们有以下几张桌子:
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;发布于 2012-11-25 14:37:28
这两名球员都得到了更大的奖金,因为两名球员自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,它可以工作。
https://stackoverflow.com/questions/13551885
复制相似问题