为什么我将来自tsv的“ NULL”日期时间条目转换为0000-00-00 00:00:00?

时间:2020-04-23 00:04:38

标签: mysql csv datetime null truncated

最近更新为mysql Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)

我正在尝试从tsv文件加载数据,以将条目添加到现有表中。一切似乎都工作正常,只是将“ NULL”值(如它的字面意思是,它不为空)转换为0000-00-00 00:00:00。没有错误,但是我得到警告“数据在行2的'beginexp'列被截断”。

这是我尝试过的方法,但无济于事:

ALTER TABLE infomegatask 
ALTER beginexp SET DEFAULT NULL; # same for field 'endhit'

ALTER TABLE infomegatask MODIFY COLUMN beginexp DATETIME NULL; #even though it was already nullable to begin with when I checked the schema

#also set sql_mode="NO_ZERO_DATE"

以下是tsv的摘要片段(敏感数据已被遮盖):

uniqueid    assignmentid    workerid    hitid   ipaddress   browser platform    language    cond    counterbalance  codeversion beginhit    beginexp    endhit  bonus   status  mode    datastring
XXXXX:YYYYY ZZZZZ   AAAAA   BBBBB   CCCCC   chrome  windows UNKNOWN 0   0   4.2 2020-04-22 16:32:24 NULL    NULL    0   4   live
mysql> LOAD DATA LOCAL INFILE 'test/DB/tsv/infomegatask_round_9g.tsv' INTO TABLE infomegatask FIELDS TERMINATED BY '\t' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES;
Query OK, 24 rows affected, 13 warnings (0.21 sec)
Records: 24  Deleted: 0  Skipped: 0  Warnings: 13

mysql> warnings
Show warnings enabled.
mysql> show warnings
    -> ;
+---------+------+------------------------------------------------+
| Level   | Code | Message                                        |
+---------+------+------------------------------------------------+
| Warning | 1265 | Data truncated for column 'beginexp' at row 2  |
| Warning | 1265 | Data truncated for column 'endhit' at row 2    |
| Warning | 1265 | Data truncated for column 'beginexp' at row 4  |
| Warning | 1265 | Data truncated for column 'endhit' at row 4    |
| Warning | 1265 | Data truncated for column 'beginexp' at row 7  |
| Warning | 1265 | Data truncated for column 'endhit' at row 7    |
| Warning | 1265 | Data truncated for column 'beginexp' at row 9  |
| Warning | 1265 | Data truncated for column 'endhit' at row 9    |
| Warning | 1265 | Data truncated for column 'beginexp' at row 10 |
| Warning | 1265 | Data truncated for column 'endhit' at row 10   |
| Warning | 1265 | Data truncated for column 'endhit' at row 14   |
| Warning | 1265 | Data truncated for column 'beginexp' at row 20 |
| Warning | 1265 | Data truncated for column 'endhit' at row 20   |
+---------+------+------------------------------------------------+
13 rows in set (0.00 sec)

我的意思是,我现在要解决的方法是将这些零日期时间更新为NULL,但这有点烦人,我只想找出问题所在。

1 个答案:

答案 0 :(得分:1)

您可以使用LOAD DATA语法转换列。

所以在您的情况下:

  LOAD DATA
  INTO TABLE  xx ( .., @beginexp , ... )
  ...
  SET beginexp = IF(@beginexp='NULL', NULL, @beginexp),
  ...