是否可以在SELECT / WHERE中使用LOAD DATA INFILE(如INSERT ... SELECT)?

时间:2014-05-16 16:52:11

标签: mysql sql

我有一张大学桌子和一张部门桌子。大学表有两列:id和name,它有一些行:

+----+-------------------------+
| id |          name           |
+----+-------------------------+
|  1 | College of Engineering  |
|  2 | College of Science      |
|  3 | College of Business     |
|  4 | College of Liberal Arts |
+----+-------------------------+

该部门有三列:id,name和college_id

+----+------+------------+
| id | name | college_id |
+----+------+------------+

现在我有一个csv文件,其中包含部门列表和学院名称:

Department of Computer Science, College of Engineering
Department of Mathematics, College of Science
Department of Histiry, College of Liberal Arts

现在我想使用LOAD DATA INFILE将csv文件加载到department表中,但departbemt表需要college_id,而csv文件只有学院名称。是否有可能在LOAD DATA INFILE条款中获得大学的ID?

谢谢!

1 个答案:

答案 0 :(得分:-1)

LOAD DATA LOCAL INFILE 'path to .csv' INTO TABLE college
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(name) 

我希望id是auto_increment。使用这个想法创建College表,之后创建一个包含csv文件中所有列的临时表。然后使用临时表和大学表,你可以填写部门表。