When updating an explicit row in MySQL is there anyway to get the UPDATE
query to return the value(s) that was actually updated?
+----+-------+---------------------+
| id | name | last_changed_values |
+----+-------+---------------------+
| 1 | Hans | (null) |
| 2 | Joe | (null) |
| 3 | Ralph | (null) |
+----+-------+---------------------+
UPDATE user SET user_name = "Bertil" WHERE user_id = 1
would enter Bertil
in last_changed_values
+----+-------+---------------------+
| id | name | last_changed_values |
+----+-------+---------------------+
| 1 | Bertil| Bertil |
| 2 | Joe | (null) |
| 3 | Ralph | (null) |
+----+-------+---------------------+
With the help of GaborSch I've created this sqlfiddle.
CREATE TRIGGER names_BU BEFORE UPDATE ON `names`
FOR EACH ROW BEGIN
SET NEW.last_changed_values = CONCAT_WS(',', IF(new.name = old.name, NULL, new.name));
END/
But this doesn't SET
last_changed_values
to new.name
(in this case Bertil
). Is there some way of picking up the new value?
Update Seems like the stored procedure was case sensitive. Changed to
SET NEW.last_changed_values = CONCAT_WS(',', IF(NEW.name = OLD.name, NULL, NEW.name));
Works as expected.