MySQL导入CSV遇Error 1265及无数据导入问题求助
解决CSV导入MySQL的截断报错与无数据导入问题
核心问题排查
你的导入语句存在几个关键错误,直接导致了当前的异常:
- 字段顺序完全不匹配:表定义包含31个字段,但导入语句仅指定20个变量,且完全跳过了表的前11个字段(
id、unnamed_id、parcel_id等)。MySQL会将CSV的第一列数据错误映射到@suite_num(INT类型字段),当CSV第一列是字符串时就会触发数据截断错误。 - 变量拼写错误:
SET语句中owner_state的赋值写成了@ownerstate(缺少下划线),这会导致该字段始终为NULL,还可能引发隐性逻辑错误。 - 字段包围符错误:
ENCLOSED BY '"'是HTML转义后的引号格式,MySQL无法识别,应该直接使用ENCLOSED BY '"'才能正确解析CSV中被引号包裹的字段。 - 换行符匹配偏差:不同系统生成的CSV换行符不同(Windows为
\r\n,Linux/macOS为\n),如果手动指定不匹配,会导致MySQL无法正确识别行边界,出现无数据导入的情况。
修正后的导入语句
确保CSV列顺序与表字段顺序完全一致(若不一致,需在INTO TABLE nash_house后显式指定字段顺序),使用以下修正后的代码:
LOAD DATA INFILE 'nash_house.csv' INTO TABLE nash_house FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY AUTO -- MySQL 8.0+支持自动推断换行符,低版本可根据系统选'\n'或'\r\n' IGNORE 1 ROWS ( id, unnamed_id, parcel_id, land_use, address, @suite_num, city, sale_date, price, legal_ref, vacant, multi_parcel, @owner_name, @owner_address, @owner_city, @owner_state, @acreage, tax_district, @num_neighbor, image, @land_val, @building_val, @total_val, @finished_area, found_type, @yr_built, ext_wall, grade, @num_bed, @num_full_bath, @num_half_bath ) SET suite_num = IF(@suite_num = '', NULL, @suite_num), owner_name = IF(@owner_name = '', NULL, @owner_name), owner_address = IF(@owner_address = '', NULL, @owner_address), owner_city = IF(@owner_city = '', NULL, @owner_city), owner_state = IF(@owner_state = '', NULL, @owner_state), -- 修正拼写错误 acreage = IF(@acreage = '', NULL, @acreage), num_neighbor = IF(@num_neighbor = '', NULL, @num_neighbor), land_val = IF(@land_val = '', NULL, @land_val), building_val = IF(@building_val = '', NULL, @building_val), total_val = IF(@total_val = '', NULL, @total_val), finished_area = IF(@finished_area = '', NULL, @finished_area), yr_built = IF(@yr_built = '', NULL, @yr_built), num_bed = IF(@num_bed = '', NULL, @num_bed), num_full_bath = IF(@num_full_bath = '', NULL, @num_full_bath), num_half_bath = IF(@num_half_bath = '', NULL, @num_half_bath);
额外排查建议
- 若使用MySQL 5.x不支持
AUTO换行符,可通过文本编辑器(如Notepad++)打开CSV,查看右下角的换行符类型(CRLF对应\r\n,LF对应\n),再手动指定LINES TERMINATED BY。 - 验证CSV表头的列数是否与表字段数一致,避免列数不匹配导致的导入失败。
- 检查
num_neighbor等数值型字段的CSV数据,若空值用NULL、N/A等占位符,需调整IF判断条件,例如IF(@num_neighbor IN ('', 'NULL'), NULL, @num_neighbor)。
内容的提问来源于stack exchange,提问作者dlrtmxmforxj
相关产品推荐
相关产品推荐

