我正在尝试使用FROM_UNIXTIME
字段将TIMESTAMP
转换为DATETIME
,DATE
和TIME
MySQL类型。我注意到 TIME
和 DATETIME
字段的值具有不同的时间信息。
值的简单选择语句,而是将其替换为表:
select FROM_UNIXTIME('1468561341') as timestamp_datetime,FROM_UNIXTIME('1468561341','%d/%m/%y') as timestamp_date, FROM_UNIXTIME('1468561341','%h:%m:%s %p') as timestamp_time
结果如下所示:
- timestamp_datetime:
2016-07-15 05:42:21.000000
- timestamp_date
15/07/16
- timestamp_time
05:07:21 AM
如何在这两列中获得一致的时间值?
问题出在字母 M 上
应该是我
mysql> select FROM_UNIXTIME(1468561341) as timestamp_datetime,FROM_UNIXTIME(1468561341,'%d/%m/%y') as timestamp_date, FROM_UNIXTIME(1468561341,'%h:%i:%s %p') as timestamp_time;
+---------------------+----------------+----------------+
| timestamp_datetime | timestamp_date | timestamp_time |
+---------------------+----------------+----------------+
| 2016-07-15 05:42:21 | 15/07/16 | 05:42:21 AM |
+---------------------+----------------+----------------+
1 row in set (0.05 sec)
你的问题是,在你的FROM_UNIXTIME中,你实际上是在告诉它去做
hours-months-seconds -AM/PM
但你想要
hours-minutes-seconds -AM/PM
所以把它改成
FROM_UNIXTIME(1468561341,'%h:%i:%s %p')
相反。
MySQL中所有时间参数和时间函数用法的参考指南:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html