我有一个.txt文件,里面有一堆格式化的数据,如下所示:
...
1 75175.18 95128.46
1 790890.89 795829.16
1 875975.98 880914.25
8 2137704.37 2162195.53
8 2167267.27 2375275.28
10 2375408.74 2763997.33
14 2764264.26 2804437.77
15 2804504.50 2881981.98
16 2882048.72 2887921.25
16 2993093.09 2998031.36
19 3004104.10 3008041.37
...
我正在尝试将每一行作为条目加载到我的数据库中的表中,其中每列是不同的字段。我无法让mySQL正确分离所有数据。我认为这个问题来自于并非所有数字都以等距的空白量分开。
以下是我到目前为止尝试过的两个查询(我也尝试过这些查询的几种变体):
LOAD DATA LOCAL INFILE
'/some/Path/segmentation.txt'
INTO TABLE clip (slideNum, startTime, endTime)
SET presID = 1;
LOAD DATA LOCAL INFILE
'/some/Path/segmentation.txt'
INTO TABLE clip
FIELDS TERMINATED BY ' '
LINES TERMINATED BY '\n'
(slideNum, startTime, endTime)
SET presID = 1;
任何想法如何让这个工作?
答案 0 :(得分:11)
这些是我们所说的“固定宽度”记录,而LOAD DATA与它们不相称。选项:
SUBSTR()
和TRIM()
将所需的列切分到最终表格中。LOAD DATA LOCAL INFILE
'/some/Path/segmentation.txt'
INTO TABLE clip
(@row)
SET slideNum = TRIM(SUBSTR(@row,1,4)),
startTime = TRIM(SUBSTR(@row,5,13)),
endTime = TRIM(SUBSTR(@row,18,13))
;
答案 1 :(得分:0)
LOAD DATA
CHARACTERSET AL32UTF8
INFILE 'DCF Master 14APR2013 VSPCFM_reduced size.txt'
INTO TABLE EMPLOYEE3
(
a = TRIM(SUBSTR(@row,1,11)),
b = TRIM(SUBSTR(@row,33,38)),
c = TRIM(SUBSTR(@row,70,86))
)