mysql LOAD DATA INFILE NA to NULL transformation

≯℡__Kan透↙ 提交于 2019-12-01 06:47:40

问题


Is there an option in the mysql LOAD DATA INFILE command, to take a .tsv file as input to mysql LOAD DATA INFILE, and transform every 'NA' field in that file to NULL in mysql?

And as a bonus, also to be able to take multiple different ones, like 'NaN','NA','--', etc. and transform all of them into 'NULL'.


回答1:


You can use variables:

LOAD DATA LOCAL INFILE 'file.tsv' INTO TABLE my_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(@col1, @col2, @col3)
SET
  col1 = CASE WHEN @col1 NOT IN ('NA', 'NaN', '--') THEN @col1 END,
  col2 = CASE WHEN @col2 NOT IN ('NA', 'NaN', '--') THEN @col2 END,
  col3 = CASE WHEN @col3 NOT IN ('NA', 'NaN', '--') THEN @col3 END

usin CASE WHEN like this:

CASE WHEN @col1 NOT IN ('NA', 'NaN', '--') THEN @col1 END

when the condition is true it will return the actual value of @col1, or NULL otherwise



来源:https://stackoverflow.com/questions/33410876/mysql-load-data-infile-na-to-null-transformation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!