5

我有一个 csv 文件,我正在使用该load local infile命令将其加载到 mysql 数据库中。

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子查询可以在设置操作的右侧使用,但似乎每次都失败:

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             |
+-------------------------+---------------------+
4

1 回答 1

3

试试这个查询 -

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)
于 2012-10-12T13:36:56.577 回答