MYSQL 更新时自定义时间

发布于 2024-11-26 13:22:37 字数 239 浏览 0 评论 0原文

我有一个包含一些行的表,其中有一个定义,如下所示:

  `metric_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

所以我真正希望它做的是在将数据插入该表时自动插入时间戳。确实如此。但我需要的是将基于 GMT 的时间写入该字段(当前服务器时间类似于 GMT+2)。

有没有办法让 MYSQL 做这样的事情?

I have a table with some rows and within them there is a definition that goes like this:

  `metric_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

So what i actually want it to do is to automatically insert a timestamp when inserting data into that table. And it does. But what I need is to write a GMT based time into that field (current server time is like GMT+2).

Is there a way to say to MYSQL to do such thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-12-03 13:22:37

如果您的服务器配置了时间和时区设置正确,然后在内部 存储在 TIMESTAMP 列中的所有时间都转换为 GMT (因为这就是 Unix 时间戳的要求)。当您检索此数据时,它们会转换回您的会话时区。如果您希望它以 GMT 时区显示,则需要在检索数据时进行转换,而不是在插入数据时进行转换。

例如,请参阅下面的控制台转储。您可以自己运行这些命令来检查。

mysql> use test;
Database changed
mysql> -- let's create a table we'll be working with
mysql> CREATE TABLE tsTable (
    -> ID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> -- let's check current time as well as timezone settings
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 16:25:51  | SYSTEM             | +02:00              |
+-----------+--------------------+---------------------+
1 row in set (0.05 sec)

mysql> -- inserting empty row to table to trigger auto timestamp
mysql> INSERT INTO tsTable VALUES (null,null);
Query OK, 1 row affected (0.00 sec)

mysql> -- looks like the time in my local timezone is stored in table
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 16:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- switching to GMT
mysql> SET SESSION time_zone = '+0:00';
Query OK, 0 rows affected (0.00 sec)

mysql> -- check current time and timezone settings again
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 14:27:53  | SYSTEM             | +00:00              |
+-----------+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> -- note: CURTIME() returns time two hours 'earlier' than before
mysql> -- let's see what's stored in the table again
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 14:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- TIMESTAMP is two hours 'earlier' than before too! Magick!

If your server time and timezone settings are configured correctly, then internally all times stored in TIMESTAMP columns are converted to GMT (since that's what Unix timestamp mandates). They're converted back to your session timezone when you retrieve this data. If you want it presented in GMT timezone, you need to do conversion while retrieving data not while inserting.

See the console dump below for example. You can run these commands yourself to check.

mysql> use test;
Database changed
mysql> -- let's create a table we'll be working with
mysql> CREATE TABLE tsTable (
    -> ID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> -- let's check current time as well as timezone settings
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 16:25:51  | SYSTEM             | +02:00              |
+-----------+--------------------+---------------------+
1 row in set (0.05 sec)

mysql> -- inserting empty row to table to trigger auto timestamp
mysql> INSERT INTO tsTable VALUES (null,null);
Query OK, 1 row affected (0.00 sec)

mysql> -- looks like the time in my local timezone is stored in table
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 16:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- switching to GMT
mysql> SET SESSION time_zone = '+0:00';
Query OK, 0 rows affected (0.00 sec)

mysql> -- check current time and timezone settings again
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 14:27:53  | SYSTEM             | +00:00              |
+-----------+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> -- note: CURTIME() returns time two hours 'earlier' than before
mysql> -- let's see what's stored in the table again
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 14:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- TIMESTAMP is two hours 'earlier' than before too! Magick!
最美的太阳 2024-12-03 13:22:37
INSERT INTO table_name(column1, metric_update_time) VALUES('dummy', CONVERT_TZ(CURRENT_TIMESTAMP,'+02:00','+03:00');

这会将插入的时间戳从 GMT+2 转换为 GMT+3

INSERT INTO table_name(column1, metric_update_time) VALUES('dummy', CONVERT_TZ(CURRENT_TIMESTAMP,'+02:00','+03:00');

This will convert the inserted timestamp from GMT+2 to GMT+3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文