2

これらのテーブルがあります:

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 |
+----+-------+------+

問題は、過去2年間(2001年と2002年)のプレーヤーが30以上のゴールを達成した場合、どうすればプレーヤーのボーナスを上げることができるかということです。たとえば、ここでは、2001年と2002年の両方でスコアが30以上だったため、id=02のプレーヤーがボーナスを受け取るようにします。

私は次の手順を使用していますが、両方のプレーヤーにボーナスが追加されるため失敗します!!!

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;
4

1 に答える 1

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を使用すると機能します。

于 2012-11-25T14:37:28.030 に答える