我有一个使用load local infile命令加载到mysql数据库中的csv文件。

csv有一组字段,这些字段是从5个值的枚举中收集的,我将需要根据这些值进行查询。为了加快查询速度(可能最多600万行),我试图在引用表上进行选择以插入ID而不是字符串值。

load data local infile 'test_file.csv'
    into table matching
    fields terminated by ','
    enclosed by '"'
    (... @match, ...)
    set
        ...
        match = select id from matchRefData where name = @match,
        ...;

根据http://dev.mysql.com/doc/refman/5.1/en/load-data.html,可以在set操作的右侧使用子查询,但每次都会失败,原因是:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near 'select id from matchRefData where name = @match,...'

版本信息:

服务器版本:5.5.25源分发
+-------------------------+---------------------+
| Variable_name           | Value               |
+-------------------------+---------------------+
| innodb_version          | 1.1.8               |
| protocol_version        | 10                  |
| slave_type_conversions  |                     |
| version                 | 5.5.25              |
| version_comment         | Source distribution |
| version_compile_machine | i386                |
| version_compile_os      | osx10.7             |
+-------------------------+---------------------+

最佳答案

试试这个查询-

LOAD DATA LOCAL INFILE 'test_file.csv'
  INTO TABLE matching
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  (..., @match, ...)
  SET `match` = (SELECT id FROM matchRefData WHERE name = @match)

关于MySQL从加载文件中的另一个表中选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12859842/

10-11 22:22
查看更多