You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 14:12:00