0

One question, is it possible to have a timestamp column that gets updated just when one column is updated (only that column)?

Thanks a lot in advance!

4

2 回答 2

5

您可以使用触发器

DELIMITER ;;

CREATE TRIGGER foo BEFORE UPDATE ON my_table FOR EACH ROW
  IF        NEW.colA <=> OLD.colA  -- has not been updated
    AND     NEW.colB <=> OLD.colB  -- has not been updated
    AND NOT NEW.colC <=> OLD.colC  -- has been updated
    -- etc.
  THEN
    SET NEW.colD = NOW();  -- update timestamp
  END IF
;;

DELIMITER ;

请注意,您的TIMESTAMP列没有启用自动更新。

于 2013-07-23T16:34:37.687 回答
1

当然。您可以将时间戳列定义为日期时间,然后在满足您的条件时使用触发器对其进行更新。

粗略的语法是:

CREATE TRIGGER trigger_name AFTER UPDATE ON table FOR EACH ROW
BEGIN
  IF(
     NOT NEW.watched_column <=> OLD.watched_column AND
     NEW.other_a <=> NEW.other_a AND
     ...
    )
  THEN
     SET NEW.timestamp_column = NOW();
  END IF;
END
于 2013-07-23T16:40:24.627 回答