mysql-sql> select time_stamp, unix_timestamp(time_stamp) from historical_data
where time_stamp > '2014-07-31' and time_stamp < '2014-10-05';
+---------------------+----------------------------+
| time_stamp | unix_timestamp(time_stamp) |
+---------------------+----------------------------+
| 2014-07-31 19:04:00 | 1406826240 |
| 2014-00-00 0:00:00 | 1406913840 |
| 2014-00-00 0:00:00 | 1407002040 |
| 2014-00-00 0:00:00 | 1407088440 |
| 2014-00-00 0:00:00 | 1407174840 |
我有一个带有TIMESTAMP列的表。我注意到当我从mysql shell查询表时,它显示许多日期为2014-00-00 0:00:00。但是当我在查询中转换为unix时间戳时,似乎数据被正确保存。好像它只是输出格式化有一些问题...(时间戳也是主键的一部分)。
这发生在2014年8月1日至2014年10月1日,然后又恢复正常输出。
不确定以下是否相关,但由于我不知道是什么原因造成这种情况发生在这里: 我有一个java应用程序,它使用gson处理来自web的json数据。数据是unix时间戳。它们将转换为Instant对象,然后使用MyBatis将其写入数据库。
现在,当从数据库中读回来时,一切似乎都正常,我得到了正确的Instant对象。它只是在mysql shell中,这似乎是一个问题。但它很奇怪而且不舒服。
以下是MySQL shell中从头开始重现的问题:
mysql-sql> select version();
+------------+
| version() |
+------------+
| 5.7.18-log |
+------------+
1 row in set (0.00 sec)
mysql-sql> create table test(`time_stamp` TIMESTAMP NOT NULL PRIMARY KEY);
Query OK, 0 rows affected (0.05 sec)
mysql-sql> insert into test (time_stamp) values ('2014-07-31');
Query OK, 1 row affected (0.01 sec)
mysql-sql> insert into test (time_stamp) values ('2014-08-15');
Query OK, 1 row affected (0.01 sec)
mysql-sql> insert into test (time_stamp) values ('2014-09-15');
Query OK, 1 row affected (0.01 sec)
mysql-sql> insert into test (time_stamp) values ('2014-10-01');
Query OK, 1 row affected (0.00 sec)
mysql-sql> select * from test;
+--------------------+
| time_stamp |
+--------------------+
| 2014-07-31 0:00:00 |
| 2014-00-00 0:00:00 |
| 2014-00-00 0:00:00 |
| 2014-10-01 0:00:00 |
+--------------------+
4 rows in set (0.00 sec)
mysql-sql> select time_stamp, unix_timestamp(time_stamp) from test;
+--------------------+----------------------------+
| time_stamp | unix_timestamp(time_stamp) |
+--------------------+----------------------------+
| 2014-07-31 0:00:00 | 1406757600 |
| 2014-00-00 0:00:00 | 1408053600 |
| 2014-00-00 0:00:00 | 1410732000 |
| 2014-10-01 0:00:00 | 1412114400 |
+--------------------+----------------------------+
4 rows in set (0.00 sec)
所以至少我知道应用层不存在问题。
答案 0 :(得分:0)
虽然我无法重现问题,但MySQL Shell中似乎存在一个错误(请参阅INSERT
中的日期和SELECT
中的结果):
Welcome to MySQL Shell 1.0.9
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type '\help', '\h' or '\?' for help, type '\quit' or '\q' to exit.
Currently in SQL mode. Use \js or \py to switch the shell to a scripting language.
mysql-sql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.18 |
+-----------+
1 row in set (0.00 sec)
mysql-sql> DROP TABLE IF EXISTS `_`.`historical_data`;
Query OK, 0 rows affected (0.03 sec)
mysql-sql> CREATE TABLE IF NOT EXISTS `_`.`historical_data` (
... `time_stamp` TIMESTAMP NOT NULL PRIMARY KEY
... );
Query OK, 0 rows affected (0.00 sec)
mysql-sql> INSERT INTO `_`.`historical_data`
... (`time_stamp`)
... VALUES
... ('2014-07-31 19:04:00'),
... ('2014-07-31 17:04:00'),
... ('2014-08-01 17:24:00'),
... ('2014-08-02 17:54:00'),
... ('2014-08-03 17:54:00'),
... ('2014-08-04 17:54:00');
Query OK, 6 rows affected (0.00 sec)
mysql-sql> SELECT
... `time_stamp`
... FROM
... `_`.`historical_data`;
+---------------------+
| time_stamp |
+---------------------+
| 2014-08-31 17:04:00 |
| 2014-08-31 19:04:00 |
| 2014-09-01 17:24:00 |
| 2014-09-02 17:54:00 |
| 2014-09-03 17:54:00 |
| 2014-09-04 17:54:00 |
+---------------------+
6 rows in set (0.00 sec)
mysql-sql> SELECT
... `der`.`time_stamp`,
... `der`.`ut`,
... FROM_UNIXTIME(`der`.`ut`)
... FROM (
... SELECT
... `time_stamp`,
... UNIX_TIMESTAMP(`time_stamp`) `ut`
... FROM
... `_`.`historical_data`
... WHERE
... `time_stamp` > '2014-07-31' AND
... `time_stamp` < '2014-10-05'
... ) `der`;
+---------------------+------------+---------------------------+
| time_stamp | ut | FROM_UNIXTIME(`der`.`ut`) |
+---------------------+------------+---------------------------+
| 2014-08-31 17:04:00 | 1406826240 | 2014-08-31 17:04:00 |
| 2014-08-31 19:04:00 | 1406833440 | 2014-08-31 19:04:00 |
| 2014-09-01 17:24:00 | 1406913840 | 2014-09-01 17:24:00 |
| 2014-09-02 17:54:00 | 1407002040 | 2014-09-02 17:54:00 |
| 2014-09-03 17:54:00 | 1407088440 | 2014-09-03 17:54:00 |
| 2014-09-04 17:54:00 | 1407174840 | 2014-09-04 17:54:00 |
+---------------------+------------+---------------------------+
6 rows in set (0.00 sec)
MySQL Shell :
mysql-sql> SELECT FROM_UNIXTIME('1406826240'),
... FROM_UNIXTIME('1406826240', '%Y-%m-%d %H:%i:%S');
+-----------------------------+--------------------------------------------------+
| FROM_UNIXTIME('1406826240') | FROM_UNIXTIME('1406826240', '%Y-%m-%d %H:%i:%S') |
+-----------------------------+--------------------------------------------------+
| 2014-08-31 17:04:00 | 2014-07-31 17:04:00 |
+-----------------------------+--------------------------------------------------+
1 row in set (0.00 sec)
MySQL命令行:
mysql> SELECT FROM_UNIXTIME('1406826240'),
-> FROM_UNIXTIME('1406826240', '%Y-%m-%d %H:%i:%S');
+-----------------------------+--------------------------------------------------+
| FROM_UNIXTIME('1406826240') | FROM_UNIXTIME('1406826240', '%Y-%m-%d %H:%i:%S') |
+-----------------------------+--------------------------------------------------+
| 2014-07-31 17:04:00.000000 | 2014-07-31 17:04:00 |
+-----------------------------+--------------------------------------------------+
1 row in set (0.00 sec)
在错误解决之前可能的解决方法:
MySQL Shell :
mysql-sql> SELECT
... CONCAT(`time_stamp`) `time_stamp`
... FROM
... `_`.`historical_data`;
+---------------------+
| time_stamp |
+---------------------+
| 2014-07-31 17:04:00 |
| 2014-07-31 19:04:00 |
| 2014-08-01 17:24:00 |
| 2014-08-02 17:54:00 |
| 2014-08-03 17:54:00 |
| 2014-08-04 17:54:00 |
+---------------------+
6 rows in set (0.00 sec)
mysql-sql> SELECT
... `der`.`time_stamp`,
... `der`.`ut`,
... CONCAT(FROM_UNIXTIME(`der`.`ut`))
... FROM (
... SELECT
... CONCAT(`time_stamp`) `time_stamp`,
... UNIX_TIMESTAMP(`time_stamp`) `ut`
... FROM
... `_`.`historical_data`
... WHERE
... `time_stamp` > '2014-07-31' AND
... `time_stamp` < '2014-10-05'
... ) `der`;
+---------------------+------------+-----------------------------------+
| time_stamp | ut | CONCAT(FROM_UNIXTIME(`der`.`ut`)) |
+---------------------+------------+-----------------------------------+
| 2014-07-31 17:04:00 | 1406826240 | 2014-07-31 17:04:00 |
| 2014-07-31 19:04:00 | 1406833440 | 2014-07-31 19:04:00 |
| 2014-08-01 17:24:00 | 1406913840 | 2014-08-01 17:24:00 |
| 2014-08-02 17:54:00 | 1407002040 | 2014-08-02 17:54:00 |
| 2014-08-03 17:54:00 | 1407088440 | 2014-08-03 17:54:00 |
| 2014-08-04 17:54:00 | 1407174840 | 2014-08-04 17:54:00 |
+---------------------+------------+-----------------------------------+
6 rows in set (0.01 sec)