在我的 MySQL 数据库中,我有一些这样的字符串:
我有点东西,Infos S.12
我要搜索的模式是:, Infos S.
,比数字(只有数字)和字符串结尾。它不区分大小写。
如何搜索并删除它?
我有这个 I'm something, Infos S.12
我想要 I'm something
。
这是我目前所拥有的:
更新 my_table SET title_col = REPLACE(title_col, SUBSTRING(title_col, LOCATE(', Infos S.', title_col), LENGTH(title_col) - LOCATE(')', REVERSE(title_col)) - LOCATE( ', Infos S. ', title_col) + 2), '') WHERE title_col LIKE '%(%)%';
剩下的怎么办?
编辑: 如果还有另一个逗号,则应将其忽略。
意思是:I'm, something, Infos S.12
(注意 I'm
后的逗号)应该得到 I'm, something
.
最佳答案
您可以使用 regexp
检查列是否具有指定的模式,然后使用 substring
将子字符串更新到 ,
.
UPDATE my_table
SET title_col = SUBSTRING(title_col,1,locate(', Infos',title_col)-1)
WHERE title_col regexp ', Infos S\\. [0-9]+$'
regexp
匹配默认不区分大小写。如果要使其区分大小写,请使用 regexp binary
。
where title_col regexp binary ', Infos S\\. [0-9]+$'
关于数据库;查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40471352/