Tyler DeWitt
Tyler DeWitt

Reputation: 23576

Update MySQL Row When Updating Using Another Table

I'm trying to use a trigger to update a column on my database when a row gets updated.

This is the trigger

CREATE
DEFINER=`root`@`localhost`
TRIGGER `mysql_development`.`update_translated_position`
BEFORE UPDATE ON `mysql_development`.`players_to_teams`
FOR EACH ROW
BEGIN
    UPDATE players_to_teams
    INNER JOIN position_translator
    ON NEW.position = position_translator.RawPosition
    SET NEW.translated_position = position_translator.NCAAposAbbrev1;
END$$

I need to "calculate" the translated_position from the raw position input (in case someone gives me a non-standard position).

I think this is locking the row, because I am getting a 1096, no table used error.

I need to update the players_to_teams row being updated using the external position_translator table.

Upvotes: 0

Views: 214

Answers (1)

eggyal
eggyal

Reputation: 125865

Use SET directly rather than UPDATE (and therefore avoid the join altogether):

SET NEW.translated_position := (
  SELECT NCAAposAbbrev1 FROM position_translator WHERE RawPosition = NEW.position
);

Upvotes: 1

Related Questions