简体   繁体   中英

MySQL “LOAD DATA INFILE” query, issue with “TERMINATED BY”

I have an hashes table with 2 columns, hash | plain hash | plain

And a text file looking like that:

acbd18db4cc2f85cedef654fccc4a4d8:foo
37b51d194a7513e45b56f6524f2d51f2:bar
4e99e8c12de7e01535248d2bac85e732:foo:bar

I'm trying to execute this query:

LOAD DATA LOCAL INFILE 'file.txt' INTO TABLE hashes COLUMNS TERMINATED BY ':' LINES TERMINATED BY '\n'

The issue is, for the hash 4e99e8c12de7e01535248d2bac85e732 , it will only insert foo , not foo:bar , because of COLUMNS TERMINATED BY ':' .

How can I make it "only split once" to fix this issue?

You could load into a user variable and use a bit of string maniplulation.

drop table if exists t;
create table t
(hash varchar(100),plain varchar(100));

LOAD DATA INFILE 'file.txt' 
INTO TABLE t 
LINES TERMINATED BY '\r\n'
(@var)
set   
        hash = substring_index(@var,':',1),
        plain = replace(@var,substring_index(@var,':',1),'')
;


select * 
from t;

+--------+----------+
| hash   | plain    |
+--------+----------+
| abc    | :def     |
| abc    | :ghi     |
| abc    | :def:ghi |
+--------+----------+
3 rows in set (0.001 sec)

Note I have used \r\n to load this properly - you should test for your environment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM