在MariaDB的表行中更改时间戳记值的正确方法是什么

时间:2019-07-18 12:03:47

标签: timestamp mariadb data-manipulation

在10.1.34-MariaDB服务器上,我有一个带有表timestamp_manipulation_experiments的数据库d,t中的每一行都包含一个称为timestamp(6)类型的timestamp字段。这些字段中的值看起来像“ 2012-04-21 10:09:20.000000”。

如何以一种引用旧值的方式正确更改字段?假设有人想将ID = 2的行的时间戳减少x分钟和y秒甚至一周来增加/减少。

尝试类似

UPDATE d.timestamp_manipulation_experiments
SET `timestamp` = `timestamp` + '0001-01-01 01:01:01' WHERE id = 2;

然后查看结果

SELECT * FROM d.timestamp_manipulation_experiments;

引起了一些错误,但是没有用。

1 个答案:

答案 0 :(得分:0)

https://mariadb.com/kb/en/library/date-time-functions/中的两个功能 已向我指出。 他们两个都工作正常。

例如:

-- add 2 Minutes and 1 Second to the current timestamp and look at the result
UPDATE d.timestamp_manipulation_experiments
SET `timestamp` = DATE_ADD(`timestamp`, INTERVAL '2:1' MINUTE_SECOND) WHERE id = 2;
SELECT * FROM d.timestamp_manipulation_experiments WHERE id = 2;

例如:

-- add 1 week to the current timestamp and look at the result
UPDATE d.timestamp_manipulation_experiments
SET `timestamp` = TIMESTAMPADD(WEEK,1,`timestamp`) WHERE id = 2;
SELECT * FROM d.timestamp_manipulation_experiments WHERE id = 2;