这是score_table的样子。
Number | Name | Score | Parent
Name
列由网络控制器设置。 Number | Name | Score | Parent
1 MJ 85 Micheal
2 Katie 60 Jay
现在我想使用load data infile
和replace
将数据修改为表格。
(.csv是1 MJ 100 Micheal
)
(这意味着用100代替MJ的分数)
所以我按如下方式编写SQL:
LOAD DATA INFILE scores.csv
REPLACE INTO TABLE score_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(Number,Score,Parent)
SET Name = #{name}
但是出现了错误:
第1行不包含所有列的数据
如何解决这个问题?谢谢!
PS:
也许应该让你知道
我尝试编写没有REPLACE
的SQL语句。(其他是相同的)
它工作(没有编译错误),但不能将分数从85改为100。
答案 0 :(得分:0)
尝试:
档案:/path/to/file/scores.csv
1,MJ,100,Micheal
MySQL命令行:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `score_table`;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `score_table` (
-> `Number` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
-> `Name` VARCHAR(255) NOT NULL,
-> `Score` INT UNSIGNED NOT NULL,
-> `Parent` VARCHAR(255) NOT NULL,
-> PRIMARY KEY (`Number`, `Name`)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `score_table`
-> (`Name`, `Score`, `Parent`)
-> VALUES
-> ('MJ', 85, 'Micheal'),
-> ('Katie', 60, 'Jay');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT
-> `Number`,
-> `Name`,
-> `Score`,
-> `Parent`
-> FROM
-> `score_table`;
+--------+-------+-------+---------+
| Number | Name | Score | Parent |
+--------+-------+-------+---------+
| 1 | MJ | 85 | Micheal |
| 2 | Katie | 60 | Jay |
+--------+-------+-------+---------+
2 rows in set (0.00 sec)
mysql> LOAD DATA INFILE '/path/to/file/scores.csv'
-> REPLACE INTO TABLE `score_table`
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\n'
-> (`Number`, @`Name`, `Score`, `Parent`)
-> SET `Name` = @`Name`;
Query OK, 2 rows affected (0.00 sec)
Records: 1 Deleted: 1 Skipped: 0 Warnings: 0
mysql> SELECT
-> `Number`,
-> `Name`,
-> `Score`,
-> `Parent`
-> FROM
-> `score_table`;
+--------+-------+-------+---------+
| Number | Name | Score | Parent |
+--------+-------+-------+---------+
| 1 | MJ | 100 | Micheal |
| 2 | Katie | 60 | Jay |
+--------+-------+-------+---------+
2 rows in set (0.00 sec)