I\'m importing a CSV into a MySQL table with LOAD DATA INFILE
. One of the table\'s fields stores zip code data, which I\'ve defined in the table structure con
For some elements (e.g. zip codes), may be better for default value to be null
, not 0
.
Assume you have a table of people called (what else) People
.
create table People (
id int,
first_name varchar(30),
last_name varchar(50),
city varchar(50),
state varchar(50),
zip int
);
Next, load your file.csv
into the People
table. Note the last line (starting with set
) where zip
is assigned null
if the value in the file is ""
(aka an empty string)
load data local infile '/path/to/file.csv'
into table People
fields
terminated by ','
enclosed by '"'
lines terminated by '\n'
(id, first_name, last_name, city, state, @zip)
set zip = if(@zip='', null, @zip);