6

MySQL 5.6.4 及更高版本支持时间列类型的小数秒。 http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html

但为了兼容性,我想忽略小数秒部分。

mysql> CREATE TABLE `t1` (`created` datetime(0) NOT NULL);
Query OK, 0 rows affected (0.41 sec)

mysql> INSERT INTO t1 set created = '2013-08-27 05:13:21.999';
mysql> INSERT INTO t1 set created = '2013-08-27 23:59:59.999';


mysql> select * from t1;
+---------------------+
| created             |
+---------------------+
| 2013-08-27 05:13:22 |
| 2013-08-28 00:00:00 |
+---------------------+
2 row in set (0.00 sec)

预期结果是“2013-08-27 05:13:21”和“2013-08-27 23:59:59”。

如何保存与以前版本的 MySQL 相同的结果?

4

2 回答 2

4

尝试创建如下表:

CREATE TABLE t1 (dt DATETIME(6) NOT NULL);

SQL小提琴

于 2013-08-27T06:30:55.920 回答
0

使用SUBTIME函数从时间中减去分数..

试试这个...从原始时间中减去微秒,然后转换为日期格式...

SELECT cast(SUBTIME(created,cast(concat('0:0:0.',(microsecond(created))) as time)) as datetime) from t1;

我测试了它..它的工作..PFB快照.. 在此处输入图像描述

于 2013-08-27T06:35:39.713 回答