Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询返回所示两个单词之间的所有文本(子字符串?)_Mysql_Sql_Substring - Fatal编程技术网

MySQL查询返回所示两个单词之间的所有文本(子字符串?)

MySQL查询返回所示两个单词之间的所有文本(子字符串?),mysql,sql,substring,Mysql,Sql,Substring,我在表格的一列中保存了一些长文本 还有一部分文字我想从中得到。。。 例如,“单词”的重要性开始和重要性结束之间的所有内容 如果我不知道那部分的确切长度,我怎么能得到它? 我不能使用子字符串,因为字符数总是不同的 有办法吗?我试图找到一些类似的函数,可以让我这样做,但找不到任何东西 A lot of words here IMPORTANTSTART some data IMPORTANTEND more words Even more words IMPORTANTSTART very imp

我在表格的一列中保存了一些长文本

还有一部分文字我想从中得到。。。 例如,“单词”的重要性开始和重要性结束之间的所有内容

如果我不知道那部分的确切长度,我怎么能得到它? 我不能使用子字符串,因为字符数总是不同的

有办法吗?我试图找到一些类似的函数,可以让我这样做,但找不到任何东西

A lot of words here IMPORTANTSTART some data IMPORTANTEND more words

Even more words IMPORTANTSTART very important data IMPORTANTEND words
也许有一种方法可以从查询输出中删除IMPORTANTSTART之前和IMPORTANTEND之后的所有内容,同时不更改数据库中的此文本?

尝试:

输出:

尝试:

输出:


您可以使用来计算子字符串的开始/结束索引。

您可以使用来计算子字符串的开始/结束索引。

我认为这是可行的。例如,我分别使用字符串abc和cba作为开始和结束分隔符

select @string := 'text abc important cba more text'
     , @start_delim := 'abc'
     , @end_delim := 'cba'
     , @start_pos := instr(@string, @start_delim) + length(@start_delim)
     , @end_pos := instr(@string, @end_delim)
     , mid(@string, @start_pos, length(@string) - @end_pos - 1)
编辑

您需要做的就是添加表,以便将其用于所有数据。此外,您还可以使用子查询来初始化相关变量和压缩表达式,以便只获得所需的内容:

select articles
     , mid(
         articles, 
         instr(articles, @start_delim) + length(@start_delim),
         length(articles) - instr(articles, @end_delim) - 1
       ) as result
from (select @start_delim := 'abc', @end_delim := 'cba') as init
   , myarchive

正如您所看到的,所需要的只是将@string替换为您想要使用的列名。

我认为这是可行的。例如,我分别使用字符串abc和cba作为开始和结束分隔符

select @string := 'text abc important cba more text'
     , @start_delim := 'abc'
     , @end_delim := 'cba'
     , @start_pos := instr(@string, @start_delim) + length(@start_delim)
     , @end_pos := instr(@string, @end_delim)
     , mid(@string, @start_pos, length(@string) - @end_pos - 1)
编辑

您需要做的就是添加表,以便将其用于所有数据。此外,您还可以使用子查询来初始化相关变量和压缩表达式,以便只获得所需的内容:

select articles
     , mid(
         articles, 
         instr(articles, @start_delim) + length(@start_delim),
         length(articles) - instr(articles, @end_delim) - 1
       ) as result
from (select @start_delim := 'abc', @end_delim := 'cba') as init
   , myarchive

如您所见,所需的只是将@string替换为您想要使用的列名。

我如何修改它以用于多行?我是否可以循环表中的多行,而不是直接插入@string:=处的行?只需插入一个子查询(类似于从myarchive中选择文章),而不是“文本abc重要cba更多文本”,子查询就会返回超过1行的错误。如何修改此子查询以用于多行?我是否可以循环表中的多行,而不是直接插入@string:=处的行?只需插入一个子查询(类似于从myarchive中选择文章),而不是“文本abc重要cba更多文本”,子查询就会返回超过1行的错误。是否可以修改此子查询以用于子查询给定的多行?结果是否可能为:IMPORTANTSTART some data IMPORTANTEND?@inside83 Yes,这是可能的,如果你写下how@inside83CONCAT'IMPORTANTSTART',子字符串\索引子字符串_INDEX@s,'IMPORTANTSTART',-1,'IMPORTANTEND',1,“IMPORTANTEND”是否可以修改它以用于子查询给定的多行?结果是否可能是:IMPORTANTSTART some data IMPORTANTEND?@inside83是的,这是可能的如果您编写how@inside83CONCAT'IMPORTANTSTART',子字符串\索引子字符串_INDEX@s,“IMPORTANTSTART”,-1,“IMPORTANTEND”,1,“IMPORTANTEND”