我有两个表:players
和history_of_players
。
我想为玩家制作某种历史记录,所以如果有人在玩家表中更新'club'
,我希望将旧数据(旧俱乐部)保存到'history_of_players'
{{1} }},'player_ID'
和'old_club'
(实际年份)。
答案 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;//