gpt4 book ai didi

mysql - 使用包含不支持格式的文字值的 CSV 数据更新 MySQL 表

转载 作者:行者123 更新时间:2023-11-29 13:26:25 24 4
gpt4 key购买 nike

最近有人删除了一个我认为对社区很有帮助的问题。我解释一下:

I am obtaining a CSV file from the Yahoo! Finance API, with which I then wish to update the following MySQL table:

CREATE TABLE yahoo.static (
symbol VARCHAR(10) NOT NULL,
exchange VARCHAR(200),
name VARCHAR(300),
capitalization DOUBLE,
div_pay_date DATE,
book_value DOUBLE,
float_shares BIGINT UNSIGNED,
PRIMARY KEY (symbol)
);

The CSV file has the following format:

"AAUKF","AAUKF","Other OTC","AAUKF","ANGLO AMERICAN OR","AAUKF",29.271B,"AAUKF","26-Apr-12","AAUKF",26.69,"AAUKF",  1134107000

Particular problems include:

  • repeated, superfluous values;

  • suffixes (e.g. K, M and B) that indicate order of magnitude; and

  • dates not formatted in a supported literal format.

How can I update the table from such a CSV file?

最佳答案

LOAD DATA内命令,可以将列分配给用户变量,然后相应地执行适当的操作:

LOAD DATA INFILE '/tmp/ystaticB.csv'
REPLACE
INTO TABLE yahoo.static
CHARACTER SET utf8 -- or however the file is encoded
FIELDS
TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"' -- quotes appear to be optional
LINES
TERMINATED BY '\r\n' -- or however newlines are encoded
(symbol,@z,exchange,@z,name,@z,@c,@z,@d,@z,book_value,@z,float_shares)
SET
capitalization = @c * POW(10, CASE RIGHT(@c,1)
WHEN 'K' THEN 3
WHEN 'M' THEN 6
WHEN 'B' THEN 9
END),
div_pay_date = STR_TO_DATE(@d, '%e-%b-%y')

关于mysql - 使用包含不支持格式的文字值的 CSV 数据更新 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20067103/

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