如何为现有的mysql存储的json对象添加name:value对

时间:2014-02-02 14:48:27

标签: mysql json keyvaluepair

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

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

并且我想添加一个新的数据对,访问: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
            )

1 个答案:

答案 0 :(得分:1)

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

暂时的一个简单方法就是使用REPLACE函数。

UPDATE table_name
SET column_name = REPLACE ( column_name, "}", ',"visited":"0"}';