我目前正在使用mysql LOAD DATA INFILE
将csv值插入名为test
的表中。到目前为止一切都很好,直到现在事情变得更加复杂。我有另一个表occupations
,其中包含occupation_id
我在表test
中使用的外键。原始csv文件仅包含以下字段First Name
,Last Name
,Age
,Date Of Birth
和Occupation
(请参阅下面的示例,其中包含值)。我想根据csv文本字段occupation_id
计算Occupation
。怎么可能呢?
csv file
中的列标题及其各自的值
+------------+-----------+-----+---------------+------------+
| First Name | Last Name | Age | Date of Birth | Occupation |
+------------+-----------+-----+---------------+------------+
| Lionel | Messi | 27 | 6/24/1987 | Soccer |
| Michael | Jordan | 51 | 2/17/1963 | Basketball |
| Lebron | James | 30 | 12/30/1984 | Actor |
+------------+-----------+-----+---------------+------------+
表occupation
+---------------+-----------------+
| occupation_id | occupation_name |
+---------------+-----------------+
| 1 | Basketball |
| 2 | Soccer |
| 3 | Actor |
+---------------+-----------------+
csv插入表test
后的结果
+------------+-----------+-----+-------------+---------------+-----------------+
| first_name | last_name | age | dob | occupation_id | occupation_name |
+------------+-----------+-----+-------------+---------------+-----------------+
| Lionel | Messi | 27 | 1987-06-24 | 2 | Soccer |
| Michael | Jordan | 51 | 1963-02-17 | 1 | Basketball |
| Lebron | James | 30 | 1984-30-12 | 3 | Actor |
+------------+-----------+-----+-------------+---------------+-----------------+
PHP / SQL - 到目前为止我的查询
$db_insert = $db_con->prepare("LOAD DATA LOCAL INFILE '".$filename."'
INTO TABLE test FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(@column1, @column2, @column3, @column4, @column5)
SET first_name=@column1, last_name=@column2, age=@column3, dob = STR_TO_DATE(@column4, '%m/%d/%Y'), occupation=@column5
");
$db_insert->execute();
答案 0 :(得分:1)
我不会尝试在LOAD DATA
声明中这样做。从理论上讲,您可以在LOAD DATA
语句中执行子查询以查找相应的occupation_id
,但即使可以,也会影响批量加载的性能。
以下是它的外观,但是如果你加载的行数超过了一些,我希望性能很糟糕:
LOAD DATA LOCAL INFILE 't.csv'
INTO TABLE test FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(@column1, @column2, @column3, @column4, @column5)
SET first_name=@column1, last_name=@column2, age=@column3,
dob = STR_TO_DATE(@column4, '%m/%d/%Y'), occupation=@column5,
occupation_id=(SELECT occupation_id FROM occupation WHERE occupation_name=@column5 LIMIT 1);
相反,我会执行LOAD DATA并将occupation_id
留空。然后在LOAD DATA
完成后,运行UPDATE
加入另一个表:
UPDATE test JOIN occupation ON test.occupation = occupation.occupation_name
SET test.occupation_id = occupation.occupation_id;
答案 1 :(得分:0)
首先,我会摆脱字段test.occupation_name
然后,您可以分两步完成:
LOAD DATA
加载到类似于csv结构的表中。我们称之为test_csv
,并使用与test
INSERT INTO test
SELECT tc.first_name, tc.last_name, tc.age, tc.dob, o.occupation_id
FROM test_csv tc
JOIN occupation o ON (tc.occupation_name=o.occupation_name)
您最终会在表test
中引用职业occupations
希望这有帮助。