MySQL通过将插入的信息迭代到另一个表

时间:2015-06-03 20:17:31

标签: mysql

我首先要说明我对MySQL触发器/函数/ SP等相当新的事实...... 因此,如果有任何关于如何询问此问题的错误,请告诉我,我会尝试获取我错过的任何信息。

我有以下表格:

aos_matchs (MatchID eventTime联盟initPlayer initAirplane事件targPlayer targAirplane武器hitCount msgID)

aos_players (MatchID Time Era VS Armament Winner redPlayers bluePlayers)

aos_playerslist (姓名ace_factor)

  1. aos_matchs.MatchID = aos_players.MatchID(MatchID指示)

  2. aos_matchs.Initiator = aos_playerslist.Name(由玩家姓名链接)

  3. aos_matchs.targPlayer = aos_playerslist.Name(由玩家姓名链接)


  4. 表格示例:

    aos_matchs:

     MatchID eventTime  Coalition initPlayer initAirplane Event  targPlayer targAirplane Weapon hitCount msgID
          56 00:00:00   red       Steve      Su-27        birth  none       none         none   none         1
          56 00:00:00   red       Greg       Su-27        birth  none       none         none   none         2
          56 00:00:00   blue      Gordon     F-15C        birth  none       none         none   none         3
          56 00:00:00   blue      George     F-15C        birth  none       none         none   none         4
          56 00:00:54   blue      George     F-15C        hit    Greg       Su-27        GUNS   36           5
          56 00:01:09   red       Greg       Su-27        dead   none       none         none   none         6
          56 00:01:13   red       Steve      Su-27        hit    Gordon     F-15C        GUNS   12           7
          56 00:01:38   blue      Gordon     F-15C        dead   none       none         none   none         8
          56 00:03:23   blue      George     F-15C        hit    Steve      Su-27        GUNS   10           9
          56 00:04:59   red       Steve      Su-27        hit    George     F-15C        GUNS   8           10
          56 00:05:08   blue      George     F-15C        dead   none       none         none   none        11
          74 00:00:00   red       John       F-15C        birth  none       none         none   none         1
          74 00:00:00   red       Adam       F-15C        birth  none       none         none   none         2
          74 00:00:00   red       Michael    F-15C        birth  none       none         none   none         3
          74 00:00:00   red       Bob        F-15C        birth  none       none         none   none         4
          74 00:00:00   blue      Berry      Su-27        birth  none       none         none   none         5
          74 00:00:00   blue      Jim        F-15C        birth  none       none         none   none         6
          74 00:00:00   blue      David      Su-27        birth  none       none         none   none         7
          74 00:00:00   blue      Randy      F-15C        birth  none       none         none   none         8
          74 00:00:48   red       John       F-15C        hit    Jim        F-15C        GUNS   16           9
          74 00:01:13   blue      Randy      F-15C        dead   none       none         none   none        10
          74 00:01:37   red       Adam       F-15C        hit    Jim        F-15C        GUNS   10          11
          74 00:01:46   red       John       F-15C        dead   none       none         none   none        12
          74 00:02:02   red       Bob        F-15C        dead   none       none         none   none        13
          74 00:02:09   red       Michael    F-15C        hit    David      Su-27        GUNS   52          14
          74 00:02:19   blue      David      Su-27        dead   none       none         none   none        15
          74 00:02:30   red       Adam       F-15C        hit    Berry      Su-27        F-15C  1           16
          74 00:02:30   blue      Berry      Su-27        hit    Adam       F-15C        Su-27  1           17
          74 00:02:30   red       Adam       F-15C        hit    Berry      Su-27        F-15C  1           18
          74 00:02:30   blue      Berry      Su-27        hit    Adam       F-15C        Su-27  1           19
          74 00:02:36   red       Adam       F-15C        dead   none       none         none   none        20
          74 00:03:38   red       Michael    F-15C        hit    Berry      Su-27        GUNS   48          21
          74 00:03:39   blue      Berry      Su-27        dead   none       none         none   none        22
          74 00:03:39   red       Michael    F-15C        hit    Berry      Su-27        GUNS   10          23
          74 00:15:09   blue      Jim        F-15C        hit    Michael    F-15C        GUNS   40          24
          74 00:15:18   red       Michael    F-15C        dead   none       none         none   none        25
    

    aos_players:

    MatchID Time                Era     VS      Armament    Winner  redPlayers  bluePlayers
    56      0000-00-00 00:00:00 modern  2vs2    GUNS        red     Steve/Greg  Gordon/George
    74      0000-00-00 00:00:00 modern  4vs4    GUNS        blue    Adam/John/Michael/Bob   Berry/Jim/David/Randy
    

    球员

    Name    ace_factor
    Steve   1
    John    1
    Adam    1
    Michael 1
    David   1
    Bob     1
    Randy   1
    Gordon  1
    George  1
    Greg    1
    

    我想用触发器捕获任何插入到Match表中的信息,然后运行查询以从MatchInfo表中获取相关信息并遍历查询结果。 逐行进行后,决定随后对Players表的更新。

    我的想法是 -

    1. 创建一个触发器,在插入信息后捕获插入操作以匹配表。

    2. 在该触发器中,对相对MatchInfo表运行查询,然后遍历查询结果的每一行并相应地更新Players表。

    3. 使用可执行上述操作的触发器示例获得任何帮助。

      编辑:按照Barmars的要求,我将表格编辑为原始布局,并附上一些原始信息,以便更好地理解。

      aos_playerslist正在使用以下触发器进行更新:

      DELIMITER $$
      
      USE `mytable`$$
      
      DROP TRIGGER /*!50032 IF EXISTS */ `UpdatePlayersList`$$
      
      CREATE
          /*!50017 DEFINER = 'admin'@'%' */
          TRIGGER `UpdatePlayersList` BEFORE INSERT ON `aos_matchs` 
          FOR EACH ROW BEGIN  
      INSERT IGNORE INTO `aos_playerslist` (`name`,`ace_factor`) VALUES (new.initplayer,1);
      END;
      $$
      
      DELIMITER ;
      

      接下来我想在aos_matchs表中插入新匹配时更新aos_playerslist表中的ace_factor,当发生这种情况时,我正在尝试通过以下查询结果更新触发器 -

      SELECT t1.initPlayer,t1.percentage/t2.full_percentage AS 'kill_percent'
                  FROM (
                      SELECT aos_matchs.MatchID,aos_matchs.`initPlayer`,SUM(CASE aos_matchs.Weapon
                      WHEN 'GUNS' THEN aos_matchs.hitcount
                      ELSE 70
                      END) AS 'percentage',dtable.dead
                      FROM aos_matchs
                      LEFT JOIN (
                          SELECT MatchID,initPlayer AS dead
                          FROM aos_matchs
                          WHERE `event`='dead') dTable
                      ON dTable.MatchID=aos_matchs.`MatchID` AND dTable.dead=aos_matchs.targPlayer
                      WHERE aos_matchs.`Event`='hit' 
                      AND dTable.dead IS NOT NULL
                      GROUP BY aos_matchs.`MatchID`,aos_matchs.`initPlayer`,dtable.dead) t1
                  LEFT JOIN (
                      SELECT aos_matchs.MatchID,SUM(CASE aos_matchs.Weapon
                      WHEN 'GUNS' THEN aos_matchs.hitcount
                      ELSE 70
                      END) AS 'full_percentage',dtable.dead
                      FROM aos_matchs
                      LEFT JOIN (
                          SELECT MatchID,initPlayer AS dead
                          FROM aos_matchs
                          WHERE `event`='dead') dTable
                      ON dTable.MatchID=aos_matchs.`MatchID` AND dTable.dead=aos_matchs.targPlayer
                      WHERE aos_matchs.`Event`='hit' 
                      AND dTable.dead IS NOT NULL
                      GROUP BY aos_matchs.`MatchID`,dtable.dead) t2
                  ON t1.MatchID=t2.MatchID 
                  AND t1.dead=t2.dead
                  WHERE t1.MatchID = <matchID>
                  AND t1.dead = <the tested dead player>
      

      然后,通过死亡测试的玩家和杀手的杀戮百分比,它将通过这些规则更新ace_factor列 -

      if ace_factor1==ace_factor2 then
      {
          if player_1 wins
          {
              new_acefactor1=ace_factor1+(ace_factor1/(ace_factor1+ace_factor2));
              new_acefactor2=ace_factor2-(ace_factor1/(ace_factor1+ace_factor2));
          }
          else if player_2 wins
          {
              new_acefactor1=ace_factor1-(ace_factor1/(ace_factor1+ace_factor2));
              new_acefactor2=ace_factor2+(ace_factor1/(ace_factor1+ace_factor2));
          }
      }
      
      else if ace_factor1>ace_factor2 then
      {
      /*Case 2a: the stronger player (player 1) wins the weaker (player 2)
      /* then the change in the ace factor of the two opponents is determined by the ace factor of 
      /*the weaker player
          if player_1 wins then
          {
              new_acefactor1=ace_factor1+(ace_factor2/(ace_factor1+ace_factor2));
              /*if the ace factor of player 2 (loser) equals "1" then it remains "1" (no 
              /*reduction)
              if ace_factor2==1 then 
              new_acefactor2=ace_factor2;
              else 
              /* if ace factor of player 2 is greater than "1" then it is reduced
              new_acefactor2=ace_factor2-(ace_factor2/(ace_factor1+ace_factor2));
          }
      
      
          /* Case 2b: the weaker player (player 2) wins the stronger player (player 1)
      
          else 
          {
          /* in that case the new ace factor of the weaker player (winner) is determined 
          /*(increased) by the value of the ace factor of his (stronger) opponent (loser)
              new_acefactor2=ace_factor2+(ace_factor1/(ace_factor1+ace_factor2));
      
          /* in that case the new ace factor of the stronger player (loser) is determined 
          /*(reduced) by the DIFFERENCE between the two ace factors 
          new_acefactor1=ace_factor1-((ace_factor1-ace_factor2)/(ace_factor1+ace_factor2));
          }
      

      我希望这很清楚,我只是在寻找一个触发器来获取每个死机的查询结果,并允许根据上述规则正确更新ace_factor列。

      这是我一直在尝试的触发器,但它不起作用,因为我可能做错了,因为我是新手一起触发:

      DELIMITER $$
      
      USE `mytable`$$
      
      CREATE
          TRIGGER `UpdateAceFactor` AFTER INSERT ON `aos_players`
          FOR EACH ROW BEGIN
          CREATE TEMPORARY TABLE mPlayers (pName VARCHAR(255));
          SET @cid =0;
          SET @mID = new.MatchID;
      
          INSERT INTO mPlayers
          SELECT initPlayer
          FROM aos_matchs
          WHERE MatchID = @mID
          AND `event`='dead';
      
          WHILE (SELECT COUNT(*) FROM mPlayers WHERE processed = 0)>0 DO
              SELECT Top 1 @cid = id FROM mPlayers WHERE processed = 0;
                  DECLARE initP VARCHAR(255);
                  DECLARE initPace INT;
                  DECLARE cur1 CURSOR FOR (
                  SELECT t1.initPlayer,t1.percentage/t2.full_percentage AS 'kill_percent'
                  FROM (
                      SELECT aos_matchs.MatchID,aos_matchs.`initPlayer`,SUM(CASE aos_matchs.Weapon
                      WHEN 'GUNS' THEN aos_matchs.hitcount
                      ELSE 70
                      END) AS 'percentage',dtable.dead
                      FROM aos_matchs
                      LEFT JOIN (
                          SELECT MatchID,initPlayer AS dead
                          FROM aos_matchs
                          WHERE `event`='dead') dTable
                      ON dTable.MatchID=aos_matchs.`MatchID` AND dTable.dead=aos_matchs.targPlayer
                      WHERE aos_matchs.`Event`='hit' 
                      AND dTable.dead IS NOT NULL
                      GROUP BY aos_matchs.`MatchID`,aos_matchs.`initPlayer`,dtable.dead) t1
                  LEFT JOIN (
                      SELECT aos_matchs.MatchID,SUM(CASE aos_matchs.Weapon
                      WHEN 'GUNS' THEN aos_matchs.hitcount
                      ELSE 70
                      END) AS 'full_percentage',dtable.dead
                      FROM aos_matchs
                      LEFT JOIN (
                          SELECT MatchID,initPlayer AS dead
                          FROM aos_matchs
                          WHERE `event`='dead') dTable
                      ON dTable.MatchID=aos_matchs.`MatchID` AND dTable.dead=aos_matchs.targPlayer
                      WHERE aos_matchs.`Event`='hit' 
                      AND dTable.dead IS NOT NULL
                      GROUP BY aos_matchs.`MatchID`,dtable.dead) t2
                  ON t1.MatchID=t2.MatchID 
                  AND t1.dead=t2.dead
                  WHERE t1.MatchID = @mID
                  AND t1.dead = @cid) kTable;
      
                  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
      
                  OPEN cur1;
      
                  the_loop: LOOP
      
                      FETCH cur1 INTO initP,initPace;
                      IF done THEN
                          LEAVE the_loop;
                      END IF;
      
                      IF initPace == (SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid) THEN
                          UPDATE `aos_playerslist`
                          SET ace_factor = (initPace+(initPace/initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)));
                          WHERE `name`=initP;
      
                          IF (SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid) > 1 THEN
                              UPDATE `aos_playerslist`
                              SET ace_factor = ((SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)-(initPace/initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)));
                              WHERE `name`=@cid;
                          END IF;
                      END IF;
      
                      IF initPace > (SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid) THEN
                          UPDATE `aos_playerslist`
                          SET ace_factor = (initPace+(initPace/initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)));
                          WHERE `name`=initP;
      
                          IF (SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid) > 1 THEN
                              UPDATE `aos_playerslist`
                              SET ace_factor = ((SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)-(initPace/initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)));
                              WHERE `name`=@cid;
                          END IF;
                      END IF;
      
                      IF initPace < (SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid) THEN
                          UPDATE `aos_playerslist`
                          SET ace_factor = (initPace+((SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)/(initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid))));
                          WHERE `name`=initP;
      
                          UPDATE `aos_playerslist`
                          SET ace_factor = ((SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)-((initPace-(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid))/initPace+(SELECT ace_factor FROM `aos_playerslist` WHERE `Name`=@cid)));
                          WHERE `name`=@cid;
                      END IF;
      
                  END LOOP the_loop;
                  CLOSE cur1;
      
              UPDATE mPlayers SET processed = 1 WHERE id = @cid
              END;
          END;
          END$$
      
      DELIMITER ;
      

0 个答案:

没有答案
相关问题