mysql 中只有一列的“更新 CURRENT_TIMESTAMP”

Bel*_*Not 5 mysql sql timestamp

我有一张test有 4 列的桌子

id     name   visited      updated_at
1      abc    2016-03-20   2016-03-20 
2      xyz    2016-03-23   2016-03-23
Run Code Online (Sandbox Code Playgroud)

当我申请以下查询时

ALTER TABLE `test` CHANGE `updated_at` `updated_at` TIMESTAMP on update 
CURRENT_TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'; 
Run Code Online (Sandbox Code Playgroud)

如果我更改name或更改visited,它会更改 updated_at

但我想它只有在visited改变时才会改变。

如何实现这一目标?谢谢你的帮助。

nas*_*kin 5

听起来你需要一个触发器才能实现这一目标。假设您的表定义如下所示:

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'
);
Run Code Online (Sandbox Code Playgroud)

updated_at...您可以实现仅visited使用触发器更新列的目标,如下所示。

-- 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 ;
Run Code Online (Sandbox Code Playgroud)

您可以通过一个相当简单的示例看到它的工作原理。

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';
Run Code Online (Sandbox Code Playgroud)


Dre*_*rew -3

drop table if exists `xyz1`;
create table `xyz1`
(   id int auto_increment primary key,
    theName varchar(20) not null,
    visited timestamp null, 
    updated_at timestamp null
);
insert `xyz1` (theName) values('abc'),('xyz');
Run Code Online (Sandbox Code Playgroud)

它对我们当前的 NULL 坐在那里感到不满意,错误 1138:

ALTER TABLE `xyz1` CHANGE `updated_at` `updated_at` TIMESTAMP 
NOT NULL DEFAULT CURRENT_TIMESTAMP; 
Run Code Online (Sandbox Code Playgroud)

让我们解决这个问题:

truncate table `xyz1`;

ALTER TABLE `xyz1` CHANGE `updated_at` `updated_at` TIMESTAMP 
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; 
-- works

insert `xyz1` (theName) values('abc'),('xyz');
select * from `xyz1`;
+----+---------+---------+---------------------+
| id | theName | visited | updated_at          |
+----+---------+---------+---------------------+
|  1 | abc     | NULL    | 2016-06-16 14:39:38 |
|  2 | xyz     | NULL    | 2016-06-16 14:39:38 |
+----+---------+---------+---------------------+


update `xyz1` set `theName`='xyz Smith' where id=2;
select * from `xyz1`;
+----+-----------+---------+---------------------+
| id | theName   | visited | updated_at          |
+----+-----------+---------+---------------------+
|  1 | abc       | NULL    | 2016-06-16 14:39:38 |
|  2 | xyz Smith | NULL    | 2016-06-16 14:41:04 |
+----+-----------+---------+---------------------+
Run Code Online (Sandbox Code Playgroud)

请参阅手册自动初始化和更新

  • 这怎么回答啊 OP 希望仅在更新“visited”时更新“updated_at”。 (5认同)
  • 这*不是*正确的答案:“updated_at”在任何列更改时都会更新 (2认同)