1

求一个 mysql 数据库替换图片的正规表达式

 1 year ago
source link: https://www.v2ex.com/t/945627
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

求一个 mysql 数据库替换图片的正规表达式

  python30 · 1 小时 42 分钟前 · 171 次点击
数据库文章 body 里有两种图片,一种是<img src="https://www.aaa.com/aa.jpg" />一种是 <img src="https://www.bbb.com/bb.jpg" /> 现在想用一种替换字符把 <img src="https://www.bbb.com/bb.jpg" />全部替换掉。就是替换为空。请问这样的 mysql 语句怎么写? 哪位朋友处理过?谢谢
5 条回复    2023-06-04 12:46:40 +08:00
totoro52

totoro52      1 小时 34 分钟前

REGEXP_REPLACE 函数, 这个是 mysql8 的新函数,低于 8 就不行
UPDATE 表名
SET body = REGEXP_REPLACE(body, 'https://www\.aaa\.com', 'https://www.bbb.com')
WHERE body LIKE '%https://www.aaa.com%';
GPT 写的 SQL ,请自测,LIKE 也可以换为 REGEXP 函数
如果低于 8 ,你还是写代码解决吧,批量读出来替换在批量去更新
totoro52

totoro52      1 小时 32 分钟前

GPT 写的这个正则应该是 hi 满足不来你这个需求,会把不是图片的也是替换,你改改加个标签上去即可。
python30

python30      1 小时 18 分钟前

@totoro52 mysql5.7 谢谢。这个改改可以用到 5.7 版本上吗?
totoro52

totoro52      3 分钟前

@python30 最低支持 8 ,只有 8 有这个函数, 你 mysql5.7 肯定不行了,用代码去解决吧
rockeyhan

rockeyhan      2 分钟前

update 'table_name' set 'field_nam' = replace ('field_name','from_str','to_str');

几年前在 MySQL 5.* 上用过,可行。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK