1

如何将新属性添加到存储在 mysql 表列中的 json 对象?现在我将 json_encode 数据存储为:

"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test@hotmail.com","barcode":"33432799181"}  

我想添加一个新的数据对,visted:0,一旦有人被访问,我想更新它。

"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test@hotmail.com","barcode":"33432799181", "visited":"0"}

如何将元素推送到现有的 json 数据?

$jsondataTmp = json_decode($core,true);
$custom = array('visited'=>'0');
$jsondataTmp[] = $custom;

但它添加了一个数组而不是每个数组

  [1] => Array
        (
            [lastname] => blah
            [firstname] => R.A.
            [function] => Manager
            [email] => test@hotmail.com
            [barcode] => 33432799181
        )

    [2] => Array
        (
           [lastname] => blah
            [firstname] => R.A.
            [function] => Manager
            [email] => test@hotmail.com
            [barcode] => 33432799181
        )

    [3] => Array
        (
            [visited] => 0
        )

并不是

[2] => Array
            (
               [lastname] => blah
                [firstname] => R.A.
                [function] => Manager
                [email] => test@hotmail.com
                [barcode] => 33432799181
[visited] => 0
            )
4

1 回答 1

2

MySQL 没有对 JSON 的原生支持 - MySQL 仍然是纯 RDBMS 系统。

目前一个简单的技巧可以是使用一个REPLACE函数。

UPDATE table_name
SET column_name = REPLACE ( column_name, "}", ',"visited":"0"}';
于 2014-02-02T15:05:01.730 回答