UPDATE上的SQL将旧数据保存到另一个表中

时间:2015-04-27 16:20:15

标签: mysql sql database triggers phpmyadmin

我有两个表:playershistory_of_players

我想为玩家制作某种历史记录,所以如果有人在玩家表中更新'club',我希望将旧数据(旧俱乐部)保存到'history_of_players' {{1} }},'player_ID''old_club'(实际年份)。

3 个答案:

答案 0 :(得分:0)

暂时没有使用MySQL,但这应该是你要找的(如果每个player_ID的播放器表中只有一行):

delimiter //
CREATE TRIGGER historise BEFORE UPDATE ON players
  FOR EACH ROW
     BEGIN
       INSERT INTO history_of_players (player_ID, old_club, year)
       SELECT player_ID, old_club, year FROM players
       WHERE player_ID = OLD.player_ID;
     END;//
delimiter ;

答案 1 :(得分:0)

您可能希望在players表上创建触发器。例如:

DELIMITER $$
CREATE TRIGGER playersHistoryTrigger
AFTER UPDATE ON players
FOR EACH ROW BEGIN
    INSERT INTO playersHistory
    VALUES (NEW.ID, NEW.Name, ...);
END$$
DELIMITER ;

答案 2 :(得分:0)

这可以通过触发器完成。

delimiter //
CREATE TRIGGER playerup BEFORE UPDATE ON players
    FOR EACH ROW
    BEGIN
        INSERT INTO history_of_players (player_ID, old_club, date)
        VALUES (NEW.player_ID, OLD.club, CURDATE());
    END;//