[英]MySQL simple query to extract substring given 2 delimiters
Specifically I need to pick the part of field label
from table tmp.label
between delimiters <!-- rwbl_1 -->
and <!-- rwbl_2 -->
where label
contains <span
in order to be able to update that field for records erroneously formatted with HTML
tags.具体而言,
<!-- rwbl_2 -->
需要从表tmp.label
<span
字段label
<!-- rwbl_1 -->
label
错误地使用HTML
标签格式化。
The result amounts to something like strip_tags
.结果相当于
strip_tags
。 This is, obviously, only possible due to the presence of the abovementioned (or similar) delimiters.显然,这仅由于上述(或类似)定界符的存在才有可能。
Here is a simple solution for the abovementioned case to extract a substring given 2 delimiters.对于上述情况,这是一个简单的解决方案,可以在给定 2 个分隔符的情况下提取 substring。
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(label,
'<!-- rwbl_1 -->', -1),
'<!-- rwbl_2 -->', 1) AS cutout
FROM tmp.label
WHERE label LIKE '<span%'
ORDER BY 1
LIMIT 1;
More abstract:更抽象:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(string_field, # field name
'delimiter_1', -1), # take the right part of original
'<!-- delimiter_2 -->', 1) # take the left part of the resulting substring
AS cutout
FROM my_table
WHERE my_condition;
And the update now works like this:现在更新的工作方式如下:
UPDATE my_table
SET string_field = SUBSTRING_INDEX(
SUBSTRING_INDEX(string_field,
'delimiter_1', -1),
'<!-- delimiter_2 -->', 1)
WHERE my_condition;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.