如何通过在 MySQL table 中用点替换逗号来避免导入数字时出现警告?
How can I avoid warnings when importing numbers by replacing comma's with a dot in a MySQL table?
我在 Mysql 中有一个 table 看起来像:
CREATE TABLE `overig`.`test`(
`ID` INT(10) AUTO_INCREMENT,
`Order_ID` DECIMAL(10) NOT NULL,
`Price_total` DECIMAL(12,2),
PRIMARY KEY (`ID`)
);
我要导入以下 CSV:
Order_ID;Price_total
145131343;118,8
145131349;
我将 CSV 数据加载到 MYSQL table:
LOAD DATA LOCAL INFILE 'test3.csv' INTO TABLE `overig`.`test` FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (
`Order_ID`, @Price_total)
SET
`Price_total` = REPLACE(@Price_total, ',', '.');
The response:
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------+
| Warning | 1366 | Incorrect decimal value: '' for column 'Price_total' at row 2 |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)
当 Price_total 为空值时如何避免此警告?
使用隐式转换。
LOAD DATA LOCAL INFILE 'test3.csv'
INTO TABLE `overig`.`test`
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(`Order_ID`, @Price_total)
SET `Price_total` = 0 + REPLACE(@Price_total, ',', '.');
我解决了
在 SQL table 中加载“ ”时出现问题。
当使用 IF 语句时 它会在没有警告的情况下正确运行。
我用过:
LOAD DATA LOCAL INFILE 'test3.csv'
INTO TABLE `overig`.`test`
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(`Order_ID`, @Price_total)
SET `Price_total` =
IF(@Price_total = '', NULL, REPLACE(@Price_total, ',', '.'));
我在 Mysql 中有一个 table 看起来像:
CREATE TABLE `overig`.`test`(
`ID` INT(10) AUTO_INCREMENT,
`Order_ID` DECIMAL(10) NOT NULL,
`Price_total` DECIMAL(12,2),
PRIMARY KEY (`ID`)
);
我要导入以下 CSV:
Order_ID;Price_total
145131343;118,8
145131349;
我将 CSV 数据加载到 MYSQL table:
LOAD DATA LOCAL INFILE 'test3.csv' INTO TABLE `overig`.`test` FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (
`Order_ID`, @Price_total)
SET
`Price_total` = REPLACE(@Price_total, ',', '.');
The response:
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------+
| Warning | 1366 | Incorrect decimal value: '' for column 'Price_total' at row 2 |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)
当 Price_total 为空值时如何避免此警告?
使用隐式转换。
LOAD DATA LOCAL INFILE 'test3.csv'
INTO TABLE `overig`.`test`
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(`Order_ID`, @Price_total)
SET `Price_total` = 0 + REPLACE(@Price_total, ',', '.');
我解决了
在 SQL table 中加载“ ”时出现问题。
当使用 IF 语句时 它会在没有警告的情况下正确运行。
我用过:
LOAD DATA LOCAL INFILE 'test3.csv'
INTO TABLE `overig`.`test`
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(`Order_ID`, @Price_total)
SET `Price_total` =
IF(@Price_total = '', NULL, REPLACE(@Price_total, ',', '.'));