Mysql 跳过 LOAD DATA INFILE 中的行

标签 mysql load load-data-infile

我想加载一个如下所示的文本文件:

11 aaa
22 bbb
# comment
33 ccc
44 bbb
# comment
55 eee

我正在使用这个命令

LOAD DATA LOCAL INFILE '/d:\test.txt' 
INTO TABLE foo 
COLUMNS TERMINATED BY ' '
IGNORE 1 LINES
(@'#')

我想用 # 跳过行的星号

最佳答案

我认为忽略 n 行并不像你期望的那样工作。 文档说

If all the lines you want to read in have a common prefix that you want to ignore, you can use LINES STARTING BY 'prefix_string' to skip over the prefix, and anything before it. If a line does not include the prefix, the entire line is skipped. Suppose that you issue the following statement:

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test
FIELDS TERMINATED BY ','  LINES STARTING BY 'xxx';

If the data file looks like this:

xxx"abc",1
something xxx"def",2
"ghi",3

The resulting rows will be ("abc",1) and ("def",2). The third row in the file is skipped because it does not contain the prefix.

The IGNORE number LINES option can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip over an initial header line containing column names:

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;

我认为最好的办法是在加载文件之前清除输入文本。

关于Mysql 跳过 LOAD DATA INFILE 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27927940/

相关文章:

javascript - 为什么图像上的加载事件不起作用?

mysql负值

php - 此选择更新是否需要回滚(不返回行)?

R - readRDS() 和 load() 无法提供与原始数据相同的 data.tables

performance - 如何开始性能测试

c# - LOAD DATA INFILE 有时找不到文件?

mysql 在几百万条记录后插入索引表需要很长时间

MySQL加载数据到文件中;错误的字符串值错误

mysql - 不能在查询中使用with

php - 在对象中使用 PDO