表字段值&外键 - LOAD DATA INFILE

时间:2014-07-08 16:32:52

标签: php mysql sql

我目前正在使用mysql LOAD DATA INFILE将csv值插入名为test的表中。到目前为止一切都很好,直到现在事情变得更加复杂。我有另一个表occupations,其中包含occupation_id我在表test中使用的外键。原始csv文件仅包含以下字段First NameLast NameAgeDate Of BirthOccupation(请参阅下面的示例,其中包含值)。我想根据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();

2 个答案:

答案 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

然后,您可以分两步完成:

  1. 将CSV文件按原样LOAD DATA加载到类似于csv结构的表中。我们称之为test_csv,并使用与test
  2. 兼容的字段名称
  3. 执行以下操作:
  4. 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

    希望这有帮助。