MySQL "LOAD DATA INFILE" 正在将未加引号的 "NULL" 字符串导入为“NULL”
MySQL "LOAD DATA INFILE" is importing unquoted "NULL" string as `NULL`
我正在使用 MySQL 5.7.35。如果我在 CSV 文件上使用 LOAD DATA INFILE
命令并将 NULL 作为 CSV 文件中未加引号的字符串值,则该值将导入为 MySQL.
中的 NULL
例如,如果我导入一个包含以下内容的 CSV 文件:
record_number,a,b,c,d,e,f
1,1,2,3,4,5,6
2,NULL,null,Null,nUlL,,"NULL"
导入的 table 将具有以下值:
+---------------+------+--------+--------+--------+--------+--------+
| record_number | a | b | c | d | e | f |
+---------------+------+--------+--------+--------+--------+--------+
| 1 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | NULL | "null" | "Null" | "nUlL" | "" | "NULL" |
+---------------+------+--------+--------+--------+--------+--------+
有没有办法在不修改 CSV 文件的情况下强制将 a 列记录 2 作为字符串导入?
更新
@Barmar 指出 MySQL 文档中有一段是关于此行为的 here:
If FIELDS ENCLOSED BY is not empty, a field containing the literal
word NULL as its value is read as a NULL value. This differs from the
word NULL enclosed within FIELDS ENCLOSED BY characters, which is read
as the string 'NULL'.
这已记录在案 here:
If FIELDS ENCLOSED BY
is not empty, a field containing the literal word NULL
as its value is read as a NULL
value. This differs from the word NULL
enclosed within FIELDS ENCLOSED BY
characters, which is read as the string 'NULL'
.
所以你需要用FIELDS ENCLOSED BY '"'
之类的东西指定引号,然后在CSV文件中写"NULL"
。
您可以检查代码中的 NULL 值并将其转换为字符串。
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(record_number, @a, @b, @c, @d, @e, @f)
SET a = IFNULL(@a, 'NULL'),
b = IFNULL(@b, 'NULL'),
c = IFNULL(@c, 'NULL'),
d = IFNULL(@d, 'NULL'),
e = IFNULL(@e, 'NULL'),
f = IFNULL(@f, 'NULL')
但是,这无法区分将 NULL
写成 \N
和 MySQL 将 NULL
视为 NULL.
我正在使用 MySQL 5.7.35。如果我在 CSV 文件上使用 LOAD DATA INFILE
命令并将 NULL 作为 CSV 文件中未加引号的字符串值,则该值将导入为 MySQL.
NULL
例如,如果我导入一个包含以下内容的 CSV 文件:
record_number,a,b,c,d,e,f
1,1,2,3,4,5,6
2,NULL,null,Null,nUlL,,"NULL"
导入的 table 将具有以下值:
+---------------+------+--------+--------+--------+--------+--------+
| record_number | a | b | c | d | e | f |
+---------------+------+--------+--------+--------+--------+--------+
| 1 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | NULL | "null" | "Null" | "nUlL" | "" | "NULL" |
+---------------+------+--------+--------+--------+--------+--------+
有没有办法在不修改 CSV 文件的情况下强制将 a 列记录 2 作为字符串导入?
更新
@Barmar 指出 MySQL 文档中有一段是关于此行为的 here:
If FIELDS ENCLOSED BY is not empty, a field containing the literal word NULL as its value is read as a NULL value. This differs from the word NULL enclosed within FIELDS ENCLOSED BY characters, which is read as the string 'NULL'.
这已记录在案 here:
If
FIELDS ENCLOSED BY
is not empty, a field containing the literal wordNULL
as its value is read as aNULL
value. This differs from the wordNULL
enclosed withinFIELDS ENCLOSED BY
characters, which is read as the string'NULL'
.
所以你需要用FIELDS ENCLOSED BY '"'
之类的东西指定引号,然后在CSV文件中写"NULL"
。
您可以检查代码中的 NULL 值并将其转换为字符串。
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(record_number, @a, @b, @c, @d, @e, @f)
SET a = IFNULL(@a, 'NULL'),
b = IFNULL(@b, 'NULL'),
c = IFNULL(@c, 'NULL'),
d = IFNULL(@d, 'NULL'),
e = IFNULL(@e, 'NULL'),
f = IFNULL(@f, 'NULL')
但是,这无法区分将 NULL
写成 \N
和 MySQL 将 NULL
视为 NULL.