mysql 导入csv空值,MySQL从CSV数据加载NULL值

当使用MySQL的LOAD DATA INFILE命令从CSV导入数据时,遇到空字段被错误地填充为0而不是NULL。通过设置变量并使用NULLIF函数,可以确保空字段正确地导入为NULL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:

1,2,3,4,5

1,2,3,,5

1,2,3

The following table was created in MySQL:

+-------+--------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+--------+------+-----+---------+-------+

| one | int(1) | YES | | NULL | |

| two | int(1) | YES | | NULL | |

| three | int(1) | YES | | NULL | |

| four | int(1) | YES | | NULL | |

| five | int(1) | YES | | NULL | |

+-------+--------+------+-----+---------+-------+

I am trying to load the data using MySQL LOAD command:

LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS

TERMINATED BY "," LINES TERMINATED BY "\n";

The resulting table:

+------+------+-------+------+------+

| one | two | three | four | five |

+------+------+-------+------+------+

| 1 | 2 | 3 | 4 | 5 |

| 1 | 2 | 3 | 0 | 5 |

| 1 | 2 | 3 | NULL | NULL |

+------+------+-------+------+------+

The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.

Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.

Thanks

S.

edit

The output of SHOW WARNINGS:

+---------+------+--------------------------------------------------------+

| Level | Code | Message |

+---------+------+--------------------------------------------------------+

| Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 |

| Warning | 1261 | Row 3 doesn't contain data for all columns |

| Warning | 1261 | Row 3 doesn't contain data for all columns |

+---------+------+--------------------------------------------------------+

解决方案

This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:

LOAD DATA infile '/tmp/testdata.txt'

INTO TABLE moo

fields terminated BY ","

lines terminated BY "\n"

(one, two, three, @vfour, five)

SET four = nullif(@vfour,'')

;

If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:

LOAD DATA infile '/tmp/testdata.txt'

INTO TABLE moo

fields terminated BY ","

lines terminated BY "\n"

(@vone, @vtwo, @vthree, @vfour, @vfive)

SET

one = nullif(@vone,''),

two = nullif(@vtwo,''),

three = nullif(@vthree,''),

four = nullif(@vfour,'')

;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值