从CSV导入列到现有的MySQL表中 [英] Import Column from CSV into existing MySQL Table
问题描述
我有一个 MySQL 表Researcher
:
ID | FirstName | Surname | Email | Position | Summary | ... + many more columns.
还有一个 CSV 文件,其中包含以下列:
And a CSV file with the columns:
Email | Summary
在MySQL表中,所有行的Summary
列均为空白,当前数据存在于CSV中.
In the MySQL table, the Summary
column is blank for all rows, the data currently exists in the CSV.
我想使用Email
将行中的Summary
列从CSV文件导入到MySQL表中.
I would like to import the Summary
column from the CSV file, into the MySQL table, using Email
to match the rows.
我该如何实现?
推荐答案
感谢您将我指向正确的花式裤子.
Thanks for pointing me in the right direction fancyPants.
我最终像这样解决了它:
I eventually solved it like this:
- 使用来自CSV的列在数据库中创建一个名为
Researcher_Temp
的新表. - 使用phpMyAdmin将LOAD DATA导入CSV到
Researcher_Temp
- 运行以下查询,将
Researcher_Temp
中的摘要数据添加到我的主Researcher
表中:
- Created a new table in the DB called
Researcher_Temp
with columns from the CSV. - Used phpMyAdmin to import the CSV using LOAD DATA into
Researcher_Temp
- Ran the following query to add the summary data from
Researcher_Temp
into my mainResearcher
table:
UPDATE Researcher
INNER JOIN Researcher_Temp on Researcher_Temp.Email = Researcher.Email
SET Researcher.Summary = Researcher_Temp.Summary;
UPDATE Researcher
INNER JOIN Researcher_Temp on Researcher_Temp.Email = Researcher.Email
SET Researcher.Summary = Researcher_Temp.Summary;
然后我删除了Temp表.
I then deleted the Temp table.
这篇关于从CSV导入列到现有的MySQL表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!