日期时间值不正确

时间:2017-06-09 09:14:16

标签: php mysql linux

我有表chart_data

CREATE TABLE `chart_data` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `type` ENUM('BID', 'ASK') NOT NULL,
    `volume` FLOAT UNSIGNED NOT NULL,
    `tokens` FLOAT UNSIGNED NOT NULL,
    `create_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `id_UNIQUE` (`id`)
)  ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=LATIN1;

当我插入一些记录时,会发生错误。例如:

# OK:
INSERT INTO `chart_data` (`type`, `volume`, `tokens`, `create_at`) VALUES ('BID', '1', '1', '2017-03-26 02:05:01');
# Error (+1h):
INSERT INTO `chart_data` (`type`, `volume`, `tokens`, `create_at`) VALUES ('BID', '1', '1', '2017-03-26 03:05:01');
# Error:
INSERT INTO `chart_data` (`type`, `volume`, `tokens`, `create_at`) VALUES ('BID', '1', '1', '2017-03-26 03:59:59');
# OK:
INSERT INTO `chart_data` (`type`, `volume`, `tokens`, `create_at`) VALUES ('BID', '1', '1', '2017-03-26 04:05:01');
# OK (+1d):
INSERT INTO `chart_data` (`type`, `volume`, `tokens`, `create_at`) VALUES ('BID', '1', '1', '2017-04-26 03:05:01');

据我了解,错误仅发生在2017-03-26的{​​{1}}。

错误堆栈跟踪:

3 am

可能是什么问题?

我的环境:

  • Executing: UPDATE `gdmv`.`chart_data` SET `create_at`='2017-03-26 03:05:01' WHERE `id`='3'; Operation failed: std::exception ERROR 1292: 1292: Incorrect datetime value: '2017-03-26 03:05:01' for column 'create_at' at row 1 SQL Statement: UPDATE `gdmv`.`chart_data` SET `create_at`='2017-03-26 03:05:01' WHERE `id`='3'
  • mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64)
  • PHP 5.6.26 (cli) (built: Sep 15 2016 15:07:33)

1 个答案:

答案 0 :(得分:0)

您尝试输入的格式是日期时间格式,因此您需要在sql架构中从timestamp更改为datetime。

因此你需要改变你的桌子:

ALTER TABLE chart_data CHANGE create_at datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP'

或者您更喜欢完整架构

CREATE TABLE `chart_data` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `type` ENUM('BID', 'ASK') NOT NULL,
    `volume` FLOAT UNSIGNED NOT NULL,
    `tokens` FLOAT UNSIGNED NOT NULL,
    `create_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `id_UNIQUE` (`id`)
)  ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=LATIN1;