Mysql 加载数据:为所有或给定的列子集设置 IF?

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

在此处将 csv 文件的导入代码导入到我的 8 列数据库中:

Load Data LOCAL InFile 'file.csv' into table myTable
Fields Terminated by ',' 
OPTIONALLY Enclosed by '"'
Lines Terminated by '\n'
IGNORE 1 Lines
(col1, col2, @var3, @var4, col5, col6, col7, col8)
Set 
col3 = if(@var3 = '', NULL, @var3),
col4 = if(@var4 = '', NULL, @var4)
;

将空条目更改为 NULL 值时效果很好,但是.... 有什么方法可以缩短 Set 部分,这样我就不必为每一列指定条件了吗? 我实际上需要这个用于上面 8 列中的 7 列,这个特定的 table 相当小。

  1. Is there any way to shorten the Set part

    是的,MySQL提供了一个shorthand函数NULLIF():

    col3 = NULLIF(@var3, '') -- etc
    
  2. so I don't have to specify a condition for each and every column?

    遗憾的是不是,尽管在您的应用程序代码中动态生成所需的 SQL 应该是相当简单的。