我有一个巨大的文本文件,需要上传到 mysql 表
数据集跨越多年,但实际数据只有月份和日期信息。然而,数据集是从最近一年开始排序的。同一日期也可能有多个数据点。
如何告诉 mysql 将其上传到表并添加正确的年份?
这是我正在处理的数据类型的示例,其中最后几位数字是月:日
Some interesting string here 11:04
Some other interesting text 10:04
........................ 10:04
........................
Some old interesting text from last year 12:02
some boring news from last year 02:01
some news from 2 years ago 12:02
最佳答案
假设您的文件从 2010 年开始,后面的日期表示下一年:
SET @last_mnth := 0, @year := 2010;
LOAD DATA INFILE '/path/to/file.txt' INTO TABLE myTable
FIELDS SEPARATED BY '\t\t' (description, @d) SET
date = CONCAT(
LEFT(CONCAT(
@mnth := SUBSTRING_INDEX(@d, ':', 1),
@date := SUBSTRING_INDEX(@d, ':', -1),
@year := @year + (
@mnth < @last_mnth
OR (@mnth = @last_mnth AND @date < @date)
),
@last_mnth := @mnth
), 0),
CONCAT_WS('-', @year, @mnth, @date)
)
;
在这里,我们将您的“时间戳”分配给用户变量@d
,然后将date
列设置为等于由空字符串串联而成的字符串(来自LEFT(..., 0)
以便将值分配给其他临时用户变量)和我们构造的日期文字。
关于mysql - 从 txt 加载重复年份的数据,仅包含天和月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13662805/