创建示例数据库和表以供使用。
create database `test`;
use `test`;
create table `test` (`value` float(10,2) null);
insert into test (value) values (null);
select value from test;
+-------+
| value |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
现在我想创建一个txt文件,当您加载它时,可以将空数据加载到测试表中。
delete from test;
这是我的名为test.txt
的数据文件,显示在vim(:set list)中。
将文件加载到表中。
LOAD DATA LOCAL INFILE "test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \w;
select value from test;
+-------+
| value |
+-------+
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
+-------+
在我的情况下,如何使用文件加载null
字段的value
数据?
如何创建这种数据文件,加载后其值字段为null
?
运行时环境:win10 + mysql-5.7。
select version();
+-----------+
| version() |
+-----------+
| 5.7.22 |
+-----------+
1 row in set (0.03 sec)
使用set list
在vim中显示test.txt文件。
\N$
\N^I$
case:未设置
不使用(value) SET value = (CASE WHEN @value IS NULL THEN NULL ELSE @value END)
加载它。
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \W;
输出:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| 0.00 |
| NULL |
+-------+
case:with set
用(value) SET value = (CASE WHEN @value IS NULL THEN NULL ELSE @value END)
加载它。
delete from test;
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
(value)SET value = (CASE WHEN @value IS NULL THEN NULL ELSE @value END) \W;
输出:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| NULL |
| NULL |
+-------+
谢谢@Schwern。
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\r\n' \W;
在没有警告的情况下,任何一种都可以正常工作。
答案 0 :(得分:1)
您可以使用\N
。
问题是您所有的输入行都有一个额外的空白字段。 Tab终止该字段,就像逗号一样。您已使用它,就像将其括在引号中一样。例如,您的第一行^Inull^I
是三个字段。第一个是空的。第二个包含null
。第三个是空的。
MySQL应该说过类似的话
Query OK, 5 rows affected, 8 warnings (0.01 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 8
您可以使用show warnings
阅读这些警告。
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'value' at row 1 |
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 2 |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 3 |
| Warning | 1265 | Data truncated for column 'value' at row 4 |
| Warning | 1262 | Row 4 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 5 |
+---------+------+---------------------------------------------------------------------------+
您需要的是这个
\N$
请注意,lines terminated by
对Unix和Windows换行符敏感。从文档中...
如果在Windows系统上生成了文本文件,则可能必须使用
LINES TERMINATED BY '\r\n'
来正确读取该文件,因为Windows程序通常使用两个字符作为行终止符。
这说明了您收到的警告。