根据返回的多个值更新多个记录

时间:2016-04-04 19:59:04

标签: oracle11g sql-update

我有下面的表名为Player_MVP_Month,它链接到我的Player表。

Here is the Table

我想更新2015年10月的MVP。每个月可能有多个MVP,2015年10月就是这种情况。根据他获得目标的次数,玩家是MVP在一个月内。 我在12月更新时没有任何问题,因为只有一名玩家有MAX COUNT,但是10月6日玩家的计数完全相同。因此,我希望在10月份更新我的6个玩家ID。 我怎么能这样做?

这就是我现在所拥有的:

UPDATE PLAYER_MVP_MONTH
SET    Player_ID = (
   SELECT Player.Player_ID
   FROM Game JOIN Player ON Game.Winning_Goal = Player.Player_ID
   WHERE Game_Date BETWEEN '01-10-2015' AND '30-10-2015'
   GROUP BY Player.Player_ID
   HAVING COUNT(Winning_Goal) = (SELECT MAX(COUNT(Winning_Goal))
                                 FROM Game
                                 WHERE Game_Date BETWEEN '01-10-2015' AND '30-10-2015'
                                 GROUP BY Winning_Goal))
WHERE PLAYER_MVP_MONTH.MVP_DATE = 'October 2015';

非常感谢任何帮助,

非常感谢。

很抱歉发布了一个链接而不是图片,但显然我需要10个声誉才能发布图片。

我试过

MERGE INTO PLAYER_MVP_MONTH USING (SELECT Player.Player_ID FROM Game JOIN Player ON Game.Winning_Goal = Player.Player_ID WHERE Game_Date BETWEEN '01-11-2015' AND '30-11-2015' GROUP BY Player.Player_ID HAVING COUNT(Winning_Goal) = (SELECT MAX(COUNT(Winning_Goal)) FROM Game WHERE Game_Date BETWEEN '01-11-2015' AND '30-11-2015' GROUP BY Winning_Goal)) ON (Player.PLAYER_ID = PLAYER_MVP_MONTH.Player_ID) WHEN MATCHED THEN UPDATE SET PLAYER_MVP_MONTH.Player_ID = Player.Player_ID ;

有人有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要在此处使用MERGE命令,该命令将根据提供的条件更新或插入数据。

http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm

更新1:您的新MERGE语句缺少WHEN NOT MATCHED THEN INSERT子句,该子句会将其他行添加到PLAYER_MVP_MONTH表中,以用于具有相同目标数的其他玩家。< / p>

这将是这样的:

MERGE INTO PLAYER_MVP_MONTH
  USING (SELECT Player.Player_ID
       FROM Game JOIN Player ON Game.Winning_Goal = Player.Player_ID
       WHERE Game_Date BETWEEN '01-11-2015' AND '30-11-2015'
       GROUP BY Player.Player_ID
       HAVING COUNT(Winning_Goal) = (SELECT MAX(COUNT(Winning_Goal))
                                     FROM Game
                                     WHERE Game_Date BETWEEN '01-11-2015' AND '30-11-2015'
                                     GROUP BY Winning_Goal))
  ON (Player.PLAYER_ID = PLAYER_MVP_MONTH.Player_ID)
  WHEN MATCHED THEN UPDATE SET PLAYER_MVP_MONTH.Player_ID = Player.Player_ID
  WHEN NOT MATCHED THEN INSERT INTO PLAYER_MVP_MONTH (MVP_DATE, PLAYER_ID) VALUES ('01-11-2015', Player.Player_ID);