如何在mysql中的currrent_timestamp中添加1小时,这是默认值? [英] How to add 1 hour to currrent_timestamp in mysql which is the default value?

查看:21
本文介绍了如何在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小时,这是默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆