【问题标题】:query not working as intended查询未按预期工作
【发布时间】:2012-11-13 03:57:01
【问题描述】:

我们有这些表格:

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;

【问题讨论】:

    标签: sql database oracle procedure


    【解决方案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 它可以工作。

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2012-11-18
      • 1970-01-01
      相关资源
      最近更新 更多