gpt4 book ai didi

mysql - 将csv文件加载到表中时如何忽略字段内的逗号

转载 作者:行者123 更新时间:2023-11-29 00:36:42 24 4
gpt4 key购买 nike

我有一个 csv 文件。我需要将内容加载到表格中。有些字段内部包含逗号,但我在这些字段中遇到错误。该命令截断此逗号中的字段。如何改进我的命令以忽略字段值中的逗号。这是我的 SQL 命令:

LOAD DATA INFILE 'C:/myfile.csv'
IGNORE
INTO TABLE db.table
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
(col1,col2,col3,col4,col5,col6);

编辑:我的 csv 文件字段未包含在 "

编辑:csv 中的数据示例:

col1  |  col2  |  col3  |  col4  |  col5  |  col6
------------------------------------------------------------
1111 | 2222 | 3333 | 4444 | 5555 | firstname, lastname

存储在 MySQL 中的 col6 为:

"firstname, lastname" .. followed by all the next fields until the column is filled and truncated.

最佳答案

你有两个选择

选项1

要求一个有效的RFC 4180 CSV 文件而不是一个好的文本文件

选项 2

其次是 LOAD DATA INFILE语法你可以这样做

LOAD DATA INFILE 'C:/myfile.csv'
IGNORE
INTO TABLE db.table
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
( col1, col2, col3, col4, col5, @firstname, @lastname )
SET col6 = CONCAT_WS( ' ', @firstname, @lastname );

关于mysql - 将csv文件加载到表中时如何忽略字段内的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13963463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com