How to add 1 hour to currrent_timestamp in mysql which is the default value?(如何在mysql中的currrent_timestamp中添加1小时,这是默认值?)
问题描述
我有一个带有时间戳字段的数据库,默认情况下采用当前时间戳,但我对时间有问题,例如如果我在 9:00 插入行,它将采用 8 作为时间戳.
I have a database with timestamp field which takes current timestamp by default, but I have problem with the time, like if I insert the row at 9:00 it will take 8 as timestamp.
所以我的问题是如何使该表中的 current_timestamp 默认增加一小时?我知道你可以用 php 做到这一点,但我更喜欢纯 mysql 解决方案.
So my question is how to make current_timestamp in that table add one hour by default? I know you can do it with php but I prefer pure mysql solution.
我的服务器时区有问题,但我不想更改它,因为我担心这可能会影响服务器上的其他数据库,而我只想更改一个数据库中的时间戳.
I have a problem with the server timezone but I don't want to change it, since I am afraid this might affect other databases on server, while I want to change timestamp only in one database.
推荐答案
你不能做 CURRENT_TIMESTAMP + INTERVAL 1 HOUR,但是你可以定义一个触发器来代替:
Simply you cannot do CURRENT_TIMESTAMP + INTERVAL 1 HOUR, but you can define a trigger instead:
CREATE TRIGGER tr_dt_table BEFORE INSERT ON your_table FOR EACH ROW BEGIN
SET NEW.datetime_field = NOW() + INTERVAL 1 HOUR;
END
并删除该字段的所有默认值(即默认设为 NULL)以避免矛盾.
And remove any default values of that field (i.e. make it NULL by default) in order to avoid contradictions.
这篇关于如何在mysql中的currrent_timestamp中添加1小时,这是默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在mysql中的currrent_timestamp中添加1小时,这是默认值?
基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
