MySQL-在插入时从相关表中插入值?

时间:2019-01-08 18:57:20

标签: mysql triggers

此语句有效:

UPDATE 
    registrations 
    JOIN players ON registrations.aa_player_id = players.a_player_id 
SET 
    registrations.`Player First`=players.player_first,    
    registrations.`Player Last`=players.player_last,    
    registrations.`Player Gender`=players.gender,
    players.date_of_birth=registrations.player_dob ;

每当将新行添加到表 registrations

中时,我都希望设置一个触发器以执行相同的操作(仅针对一行)

我已经尝试过此代码

创建触发器lookup_player
   在registrations上插入之前
   每行    更新注册

设置
    new。Player Last = players.player_last,
    新增。Player First = players.player_first,      new.registrations。Player Gender = players.gender,
    new.player_dob = players.date_of_birth
  其中new.aa_player_id = players.a_player_id;

但是当我向注册

添加新行时出现错误

1054-“ where子句”中的未知列“ players.a_player_id”

但是那列确实存在

1 个答案:

答案 0 :(得分:0)

想通了

CREATE  TRIGGER `lookup player`   

registrations上插入之前    每行
   设置
   new。Player Last =(从有new.aa_player_id = players.a_player_id的玩家中选择players.player_last),
   new Player First =(从有new.aa_player_id = players.a_player_id的玩家中选择players.player_first),
   new。Player Gender =(从有new.aa_player_id = players.a_player_id的玩家中选择player.gender),
   new.player_dob =(从new.aa_player_id = players.a_player_id的玩家中选择players.date_of_birth);