MySQL:导入的CSV文件将空值转换为0

jvidinwx  于 2023-03-27  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我已经检查了一些关于这个问题的旧讨论,它似乎并没有为我解决这个问题。唯一的解决方案是在这里:MySQL load NULL values from CSV data但我有更多的变量比建议在这种情况下。
我在用

mysql  Ver 8.0.32-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))

我想和你检查任何其他方式来上传我的csv文件,并保持NULL值为NULL,而不是自动转换为0

创建表:

CREATE TABLE CovidDeaths
(iso_code   varchar(100)    ,
continent   varchar(100)    ,
location    varchar(100)    ,
date    varchar(100)    ,
total_cases int ,
new_cases   int ,
new_cases_smoothed  int ,
total_deaths int,
new_deaths  int ,
new_deaths_smoothed int ,
total_cases_per_million FLOAT   ,
new_cases_per_million   FLOAT   ,
new_cases_smoothed_per_million  FLOAT   ,
total_deaths_per_million    FLOAT   ,
new_deaths_per_million  FLOAT   ,
new_deaths_smoothed_per_million FLOAT   ,
reproduction_rate   FLOAT   ,
icu_patients    int ,
icu_patients_per_million    FLOAT   ,
hosp_patients   int ,
hosp_patients_per_million   FLOAT   ,
weekly_icu_admissions   int ,
weekly_icu_admissions_per_million   FLOAT   ,
weekly_hosp_admissions  int ,
weekly_hosp_admissions_per_million  FLOAT   ,
new_tests   int ,
total_tests int ,
total_tests_per_thousand    FLOAT   ,
new_tests_per_thousand  FLOAT   ,
new_tests_smoothed  int ,
new_tests_smoothed_per_thousand FLOAT   ,
positive_rate   FLOAT   ,
tests_per_case  FLOAT   ,
tests_units int ,
total_vaccinations  int ,
people_vaccinated   int ,
people_fully_vaccinated int ,
new_vaccinations    int ,
new_vaccinations_smoothed   int ,
total_vaccinations_per_hundred  FLOAT   ,
people_vaccinated_per_hundred   FLOAT   ,
people_fully_vaccinated_per_hundred FLOAT   ,
new_vaccinations_smoothed_per_million   FLOAT   ,
stringency_index    int ,
population  int ,
population_density  FLOAT   ,
median_age  FLOAT   ,
aged_65_older   int ,
aged_70_older   int ,
gdp_per_capita  FLOAT   ,
extreme_poverty int ,
cardiovasc_death_rate   FLOAT   ,
diabetes_prevalence FLOAT   ,
female_smokers  FLOAT   ,
male_smokers    FLOAT   ,
handwashing_facilities  int ,
hospital_beds_per_thousand  FLOAT   ,
life_expectancy FLOAT   ,
human_development_index FLOAT);

文件上传:

LOAD DATA LOCAL INFILE '/home/gyahia/Desktop/BootCamp/CovidVaccinations.csv'
INTO TABLE CovidVaccinations
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
-- OPTIONALLY ENCLOSED BY ''
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Select语句的结果

谢谢你的帮助

omhiaaxx

omhiaaxx1#

您有两种选择:

  • 编辑CSV输入文件,将应该为NULL的值更改为\NLOAD DATA INFILE将转义序列加载为NULL。
  • 使用NULLIF()函数将''转换为NULL,如您链接到的其他答案中所述。您有比答案显示的更多的变量,但您只有59个,对吗?输入这些变量需要更长的时间,但它会工作。

如果您不喜欢键入太多内容,可以从列名列表生成代码。
你可以在一个查询中得到这个列表,如下所示:

mysql> select column_name 
 from information_schema.columns 
 where table_name ='CovidDeaths';

您可以将它们与GROUP_CONCAT()结合使用,为LOAD DATA INFILE生成正确的代码行:

select concat(
 'LOAD DATA LOCAL INFILE ''/home/gyahia/Desktop/BootCamp/CovidVaccinations.csv''
  INTO TABLE CovidVaccinations 
  FIELDS TERMINATED BY '','' 
  ENCLOSED BY ''"'' 
  LINES TERMINATED BY ''\\n'' (',
group_concat(concat('@', column_name)),
') 
SET ',
group_concat(concat(column_name, '=NULLIF(@', column_name, ', '''')')),
' IGNORE 1 ROWS;') as _load_data_stmt
from information_schema.columns where table_name='CovidDeaths'

相关问题