MYSQL JSON_SET添加反斜杠

时间:2019-02-10 09:37:10

标签: mysql arrays json mysql-json

我想更新数据库中的JSON数组

我有这个数组:

Array
(
    [id] => 34
    [var1] => val1
    [var2] => val2
    [var3] => val3
)

我需要通过ID更新json

查询示例:

JOSN_SET(`json_arr`,'$."34"','{"id":"34","var1":"val1","var2":"val2","var3":"val3"}')

预期结果:

{"34":{"id":"34","var1":"val1","var2":"val2","var3":"val3"}}

结果:

{"34":"{\"id\":\"34\",\"var1\":\"val1\",\"var2\":\"val2\",\"var3\":\"val3\"}"}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您应将其投射为JSON,例如:

JOSN_SET(`json_arr`,'$."34"',CAST('{"id":"34","var1":"val1","var2":"val2","var3":"val3"}' AS JSON))