溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL 5.6中timestamp和datetime區(qū)別有哪些

發(fā)布時間:2021-11-06 09:01:35 來源:億速云 閱讀:147 作者:小新 欄目:MySQL數(shù)據(jù)庫

小編給大家分享一下MySQL 5.6中timestamp和datetime區(qū)別有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

MySQL會根據(jù)當(dāng)前時區(qū)轉(zhuǎn)化TIMESTAMP值,在查詢時候會根據(jù)當(dāng)前時區(qū)來處理。

mysql> create table test2(a int(20));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into test2 values(1466929145);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test2;
+------------+
| a          |
+------------+
| 1466929145 |
+------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(a) from test2;
+---------------------+
| from_unixtime(a)    |
+---------------------+
| 2016-06-26 08:19:05 |
+---------------------+
1 row in set (0.00 sec)

mysql> insert into test2 values(14669291450);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select from_unixtime(a) from test2;
+---------------------+
| from_unixtime(a)    |
+---------------------+
| 2016-06-26 08:19:05 |
| 2038-01-19 03:14:07 |
+---------------------+
2 rows in set (0.00 sec)

mysql> insert into test2 values(14669291450);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'a' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(a) from test2;
+---------------------+
| from_unixtime(a)    |
+---------------------+
| 2016-06-26 08:19:05 |
| 2038-01-19 03:14:07 |
| 2038-01-19 03:14:07 |
+---------------------+
3 rows in set (0.00 sec)

查看當(dāng)前時區(qū)
mysql> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | +00:00 |
+---------------+--------+
1 row in set (0.00 sec)

更改會話時區(qū)參數(shù)后,可以看到查詢出來的時間發(fā)生了變化
mysql> set session time_zone='+01:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select from_unixtime(a) from test2;
+---------------------+
| from_unixtime(a)    |
+---------------------+
| 2016-06-26 09:19:05 |
| 2038-01-19 04:14:07 |
| 2038-01-19 04:14:07 |
+---------------------+
3 rows in set (0.00 sec)

datetime不受時區(qū)的影響
mysql> create table test3(a datetime);
Query OK, 0 rows affected (0.10 sec)

mysql> select * from test3;
Empty set (0.00 sec)

mysql> insert into test3 values(now());
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from test3;
+---------------------+
| a                   |
+---------------------+
| 2017-02-13 10:02:20 |
+---------------------+
1 row in set (0.00 sec)

mysql> set session time_zone='+10:00';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | +10:00 |
+---------------+--------+
1 row in set (0.00 sec)

mysql> select * from test3;
+---------------------+
| a                   |
+---------------------+
| 2017-02-13 10:02:20 |
+---------------------+
1 row in set (0.00 sec)

以上是“MySQL 5.6中timestamp和datetime區(qū)別有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI