我试图从mysql中的LONGTEXT中选择特定范围的行。
例如,假设我有一个名为log_data的LONGTEXT列,其中包含以下文本:
INFO this is the first line
INFO this is the second line
INFO this is the third line
INFO this is the fourth line
INFO this is the fifth line我希望能够选择包含2-4行的第2-4行(但这些值可能会改变,有时我可能希望在45行长的条目中从第15-26行中选择,等等)。
有点像
select LINES(log_data, 2, 4) as log_data from logs where id = 7;应导致
INFO this is the second line
INFO this is the third line
INFO this is the fourth line注释:行总是用
\n分隔,从不用\r\n分隔。
我知道我可以使用SUBSTRING_INDEX选择0到X行,但据我所知,这并不允许我选择起跑线。
发布于 2019-09-22 16:41:21
如果我们在函数中提供负的计数,它将从右到左计数,并将子字符串取到分隔符的右侧。
所以,用一些数学,为了得到第二(2)到第四(4)行:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(log_data, '\n', 4),
'\n',
-(4 - 2 + 1)
)同样,对于第15-26行,如下所示:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(log_data, '\n', 26),
'\n',
-(26 - 15 + 1)
)p线到q线的一般公式是:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(log_data, '\n', q),
'\n',
-(q - p + 1)
)https://stackoverflow.com/questions/58051125
复制相似问题