日期列的默认日期格式YYYY-MM-DD HH:MM:SS
在 MySQL 中。
我尝试从中加载的数据文件有一个日期字段,其中包含日期DD-MON-YY HH:MM:SS
格式。当我使用命令加载此文件时LOAD DATA
,数据库会感到困惑,只会将所有日期条目设为0000-00-00 00:00:00
NULL
这是我使用STR_TO_DATE
选项所做的测试,它不起作用。
测试 infile (test_temp.csv)
c1, c2
07-JUN-12 22:50:19, "abc"
07-JUN-13 22:50:19, "bcd"
测试表(temp_test)
describe temp_test;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| c1 | datetime | YES | | NULL | |
| c2 | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
数据加载命令:
load data
infile '/var/lib/mysql/DataSet-1/temp_test.csv'
ignore
into table temp_test
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 lines
(@var_c1,c2)
set c1 = STR_TO_DATE(@var_c1,'%d-%b-%y %h:%i:%s');
输出
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
MySQL> show warnings;
+-------+------+-------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------+
| Error | 1411 | Incorrect datetime value: '07-JUN-12 22:50:19' for function str_to_date |
| Error | 1411 | Incorrect datetime value: '07-JUN-13 22:50:19' for function str_to_date |
+-------+------+-------------------------------------------------------------------------+
MySQL> select * from temp_test;
+------+------+
| c1 | c2 |
+------+------+
| NULL | abc |
| NULL | bcd |
+------+------+
有问题吗
- 输入日期列(应该是
07-JUN-12
或07-Jun-12
)或 - 使用我的格式字符串 (
%d-%b-%y
) 或 - 还有什么?