sql - Mysql Load Data: SET IF for all or given subset of columns? -


got working import code csv files 8-column database here:

load data local infile 'file.csv' table mytable fields terminated ','  optionally enclosed '"' lines terminated '\n' ignore 1 lines (col1, col2, @var3, @var4, col5, col6, col7, col8) set  col3 = if(@var3 = '', null, @var3), col4 = if(@var4 = '', null, @var4) ; 

it's working fine in changing empty entries null values, but.... there way shorten set part don't have specify condition each , every column? need 7 of 8 columns above, , particular table rather small.

  1. is there way shorten set part

    yes, mysql provides shorthand function nullif():

    col3 = nullif(@var3, '') -- etc 
  2. so don't have specify condition each , every column?

    sadly not, although should trivial generate desired sql dynamically in application code.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -