MySQL替换语法 - 包装特定的文本

时间:2012-02-21 16:27:34

标签: mysql

在一个表中,有一个params行,它有一些JSON编码数据 - 如下:

{"categories":211,"singleCatOrdering":"","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"","show_page_heading":0,"page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}

我需要做的是在[]参数周围包裹categories - 看起来像这样:

{"categories":[211],"singleCatOrdering":"","menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"page_title":"","show_page_heading":0,"page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}

我尝试了以下(和其他一些失败的)并且没有骰子:

UPDATE j17_menu SET params = REPLACE(params,'"categories":%,','"categories":[%],') WHERE component_id = 10021;

我是否可能使用通配符错误?任何朝着正确方向的推动都将是一个巨大的帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

这个将独立于上下文工作

UPDATE j17_menu 
SET params=CONCAT (
    SUBSTR(params,1,LOCATE('"categories":',params)),
    '"categories":[',
    substr(
        params,
        LOCATE('"categories":',params)+13,
        LOCATE(',',params,LOCATE('"categories":',params))-LOCATE('"categories":',params)-13
    ),
    ']',
    substr(params,LOCATE(',',params,LOCATE('"categories":',params)))
)
WHERE component_id = 10021;