数据库
首页 > 数据库> > Mysql从加载infile中的另一个表中选择

Mysql从加载infile中的另一个表中选择

作者:互联网

我有一个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子查询可以在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,load-data-infile
来源: https://codeday.me/bug/20190704/1373726.html