简体   繁体   English

使用phpMyAdmin将带有部分数据的制表符分隔的csv文件导入mysql表

[英]import tab separated csv file with partial data into mysql table with phpMyAdmin

I have a MySQL table which has about 30 columns. 我有一个约有30列的MySQL表。 I have a CSV file which has about 10 columns. 我有一个约有10列的CSV文件。

I want to use phpMyAdmin to update the data from the CSV file in the database without changing the rest of the data. 我想使用phpMyAdmin从数据库中的CSV文件更新数据,而不更改其余数据。

For example, the MySQL would be: 例如,MySQL将是:

id, code,  name,     description, a,   b,   c,   x,  y,   z
10, rbmls, Joe Blow, Neighbor,    100, 200, 500, 80, 800, 10

The CSV could contain: CSV可能包含:

id[TAB]code [TAB]a [TAB]b  [TAB]c
10[TAB]rbmls[TAB]10[TAB]300[TAB]0

I want to match the data in the CSV and the MySQL table by ID and then overwrite the data from the CSV in the MySQL table without deleting anything else. 我想按ID匹配CSV和MySQL表中的数据,然后覆盖MySQL表中CSV中的数据,而不删除其他任何内容。

The desired result of the above example would be: 上述示例的预期结果将是:

id, code, name, description, a, b, c, x, y, z
10, rbmls, Joe Blow, Neighbor, 10, 300, 0, 80, 800, 10

How do I do that in phpMyAdmin? 如何在phpMyAdmin中做到这一点?

LOAD DATA INFILE has a column-specification at the end. LOAD DATA INFILE在末尾具有列规范。 see here 看这里

LOAD DATA INFILE ... 
INTO TABLE ... 
FIELDS TERMINATED BY '\t'
IGNORE 1 LINES
(id, code, a, b, c)

From MySQL-Doc: 从MySQL-Doc:

By default, when no column list is provided at the end of the LOAD DATA INFILE statement, input lines are expected to contain a field for each table column. 默认情况下,当在LOAD DATA INFILE语句的末尾未提供任何列列表时,输入行应包含每个表列的字段。 If you want to load only some of a table's columns, specify a column list: LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...); 如果只想加载表的某些列,请指定一个列列表:LOAD DATA INFILE'persondata.txt'INTO TABLE persondata(col1,col2,...);

Alternative 替代

Load the data in a temporary table and do an INSERT ... UPDATE from there to the main-table. 将数据加载到临时表中,然后从那里对主表执行INSERT ... UPDATE Something like that: 像这样:

LOAD DATA INFILE ... INTO TABLE `temp` ...

INSERT INTO mainTable (id, code, a, b, c)
SELECT * FROM `temp`
  ON DUPLICATE KEY UPDATE code=VALUES(code), a=VALUES(a), b=VALUES(b), c=VALUES(c);

See here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM