reformed
reformed

Reputation: 4780

In MySQL, how do you automatically set a value for one column when updating another in the same row?

For the example table below, is there a way to configure the table to auto update the PreviousValue column with the data in the Value column when UPDATE occurs?

table Settings
(
    Setting varchar(255),
    Value varchar(255),
    PreviousValue varchar(255)
)

Upvotes: 1

Views: 84

Answers (2)

xdazz
xdazz

Reputation: 160833

Without trigger, you could also achieve this with below:

UPDATE Settings SET PreviousValue = Value, Value = :new_value WHERE Setting = :setting

Upvotes: 2

eggyal
eggyal

Reputation: 125855

CREATE TRIGGER my_trigger BEFORE UPDATE ON my_table FOR EACH ROW
  SET NEW.PreviousValue = OLD.Value;

Upvotes: 4

Related Questions