MySQL执行带链式REPLACE的UPDATE语句报1064语法错误如何解决?
MySQL UPDATE语法报错问题修复
错误原因
- UPDATE语句顺序错误:MySQL的UPDATE语法要求
SET子句必须放在WHERE子句之前,你将WHERE条件写在了SET前面,直接触发语法报错。 - 多余的SET关键字:同一条UPDATE语句里仅需写一次SET关键字,多个字段赋值(或同一字段多次修改)用逗号分隔即可,不需要重复写SET。
- 存在无效语法行:代码中包含单独的
SET description无赋值逻辑的无效行,也会触发语法校验失败。
正确写法
你可以选择嵌套REPLACE函数单次赋值的写法,执行效率更高:
UPDATE media SET description = REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(description, 'https://images.weserv.nl/?url=http', 'http'), '[img]https://blu.xyz/favicon.ico', '[img=35]https://blu.xyz/favicon.ico' ), '[img]http', '[img=400]http' ), 'https://images.weserv.nl/?url=', 'https://' ), '&w=35', '' ), '&w=400', '' ), '&h=400', '' ), '&w=300', '' ), '&h=300', '' ) WHERE id = 63212 AND description LIKE '%https://images.weserv.nl/%';
也可以选择分步赋值的写法,可读性更强:
UPDATE media SET description = REPLACE(description, 'https://images.weserv.nl/?url=http', 'http'), description = REPLACE(description, '[img]https://blu.xyz/favicon.ico', '[img=35]https://blu.xyz/favicon.ico'), description = REPLACE(description, '[img]http', '[img=400]http'), description = REPLACE(description, 'https://images.weserv.nl/?url=', 'https://'), description = REPLACE(description, '&w=35', ''), description = REPLACE(description, '&w=400', ''), description = REPLACE(description, '&h=400', ''), description = REPLACE(description, '&w=300', ''), description = REPLACE(description, '&h=300', '') WHERE id = 63212 AND description LIKE '%https://images.weserv.nl/%';
内容的提问来源于stack exchange,提问作者HDVinnie
相关产品推荐
相关产品推荐

