wie Sie das Handbuch Siehe Sounds brauche einen Auslöser, um das zu erreichen. Ihre Tabellendefinition Unter der Annahme, sieht so aus:
drop table if exists test;
create table test(
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL DEFAULT '',
visited DATETIME NOT NULL DEFAULT '0000-00-00',
updated_at DATETIME NOT NULL DEFAULT '0000-00-00'
);
... Sie Ihr Ziel der Aktualisierung updated_at
nur für die visited
Säule mit einem Trigger, wie so erreichen kann.
-- Create a trigger that will update the updated_at column iff visited changes.
drop trigger if exists upd_test;
delimiter //
create trigger upd_test BEFORE UPDATE ON test
FOR EACH ROW
BEGIN
IF ( (old.visited is not null and new.visited is not null and old.visited <> new.visited)
OR (old.visited is null and new.visited is not null)
OR (old.visited is not null and new.visited is null) ) THEN
SET NEW.updated_at = CURRENT_TIMESTAMP;
END IF;
END;//
delimiter ;
Und Sie können es mit einem ziemlich geradlinigen Beispiel arbeiten sehen.
insert into test(name) values ("Bran"), ("Catelyn"), ("Daenerys"), ("Eddard");
-- this statement will not cause updated_at to be updated
update test
set name = 'Jon'
where name = 'Eddard';
-- this statement will cause updated_at to be updated, via the trigger
update test
set visited = '2016-06-16'
where name = 'Jon';
Wie ist das eine Antwort? Das OP möchte, dass das 'updated_at' aktualisiert wird, wenn' visited' nur aktualisiert wird. – JDrake