MySQL在LOAD DATA INFILE语句中使用WHERE来自CSV / TXT

时间:2013-08-15 13:22:31

标签: mysql mysql-workbench

我有一个txt文件,如下所示:

Col1, Col2, Col3
1, foo0, bar0
1, foo1, bar1
2, foo2, bar2
3, foo3, bar3

我想使用类似的查询将txt文件导入数据库:

LOAD DATA INFILE '/tmp/sample.txt'
INTO TABLE sample
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

但是只想要来自txt文件的记录,其中Col1的值为1,我只想在样本表中使用Col1和Col2。如何使用WHERE子句创建查询以确定Col1的值为1,然后仅将txt中的前两列放入样本表中?

1 个答案:

答案 0 :(得分:1)

查看mysql加载数据的infile语法

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]

它似乎不支持where子句。您将不得不在之后删除这些不需要的记录,或者执行某种形式的命令行过滤,然后在mysql中加载过滤后的数据。