我正在将 MySQL 5.5 物理主机数据库迁移到 MySQL 5.6 AWS Aurora 数据库。我注意到,当使用 INTO OUTFILE
将数据写入文件时,5.5 将 NULL
值写入 '\N'
并将空字符串写入 >''
。然而,5.6 将空字符串和 NULL 都写为 ''
。
查询
SELECT * FROM $databasename.$tablename INTO OUTFILE $filename 字符集 utf8 字段转义由 '\\\\' 终止于 $delimiter;
我找到了关于此的官方文档:
https://dev.mysql.com/doc/refman/5.6/en/load-data.html
With fixed-row format (which is used when FIELDS TERMINATED BY and FIELDS ENCLOSED BY are both empty), NULL is written as an empty string. This causes both NULL values and empty strings in the table to be indistinguishable when written to the file because both are written as empty strings. If you need to be able to tell the two apart when reading the file back in, you should not use fixed-row format.
如何将 NULL
导出为 '\N'
?
最佳答案
How do I export
NULL
as'\N'
?
首先,这很奇怪,为什么你要这样做?但是,如果由于某种原因您想以这种方式导出它,那么您必须将查询从 select *
更改为使用 CASE
表达式,例如
select
case when col1 is null then '\\N' else col1 end as col1,
...
from $databasename.$tablename....
正如评论所述,您也可以使用 IFNULL()
函数或 COALESCE()
函数来达到相同目的。
关于MySQL 5.6,如何将NULL导出为\N?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39713988/