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小时,这是默认值?


基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在多列上分布任意行 2021-01-01