选择 concat 子字符串并在 MySQL 中替换



我有一个.sql文件,需要删除所有多余的字符,只留下文本。所以"里面的任何东西。 列中 TEXT 字段的一个示例是

A:1:{i:0;s:9:"在这里测试单词";}

a:1:{i:0;s:11:"在这里也测试单词";}

所以我想要这些词。在这里测试单词。在这里测试单词。全部保留在文本字段中。

我最初是这样的东西。

  UPDATE `questions`
  SET answer = REPLACE(REPLACE(REPLACE(REPLACE(answer, 'a:1', ''), 's:4', ''), 'i:0', ''), ',', '')

但后来很快意识到 s:4 有 s:5、s:16 等。所以这是行不通的。我的下一个尝试是使用 concat 并删除一定数量的以 a:1 开头的字符。 我能够做一个工作选择。但无法让替换工作。下面你可以看到工作选择。

SELECT CONCAT('tt',
          SUBSTRING(`answer`, -LOCATE('a:1', `answer`)+15)
   ) from `questions`;

这是我尝试让替换使用它。但我被困住了。我愿意接受任何建议,以防我在这里走错了方向。

SELECT CONCAT(REPLACE('tt',
          SUBSTRING(`answer`, -LOCATE('a:1', `answer`)+15))
   ) from `questions`;

substring_index()怎么样?

UPDATE `questions`
    SET answer = substring_index(substring_index(answer, '"', 2), '"', -1)

你可以做这样的事情:

select substr(with_end, 1, locate('"', with_end )-1) as 'str'
from (
  select substr(answer, locate('"', answer )+1) 'with_end'
  from questions
) as q

最新更新