mysql 时间转换问题

Issue with mysql time converstion

我正在尝试将文件读取到 mysql 数据库,有这样的时间列格式

05/20/16 01:54:52.797 AM => month/day/year hours:minutes:seconds.millisecond AM/PM

我正在使用以下代码,但我无法正确阅读它

Drop table temp;
CREATE TABLE temp LIKE messages;    
load data infile 'C:/xampp/htdocs/gdxg/test_uploads/test.txt' into table temp fields terminated by '\t' enclosed by '"' lines terminated by '\n' (@Time, State, Name,Pri,Value,)
SET Time = STR_TO_DATE(@Time,'%m/%d/%y %H:%i:%s.%f');
select * from temp limit 10;

它不读 AM/PM

如果我尝试这样做 (@Time,'%m/%d/%y %H:%i:%s.%f %p),请阅读 null?

您错误地使用了 hour 说明符。

说明符描述

  1. '%H' - 小时数在 (00..23)
  2. 范围内
  3. '%h' - 时间范围 (01..12)
  4. '%p' - 上午或下午

在您的查询中将 H 更改为 h,使用 '%p' 应该可以。

示例

mysql> select @t;
+--------------------------+
| @t                       |
+--------------------------+
| 05/20/16 01:54:52.797 AM |
+--------------------------+
1 row in set (0.00 sec)

mysql> select STR_TO_DATE( @t,'%m/%d/%y %h:%i:%s.%f %p' ) as t;
+----------------------------+
| t                          |
+----------------------------+
| 2016-05-20 01:54:52.797000 |
+----------------------------+
1 row in set (0.00 sec)

阅读文档