mysql 5.7将键/值附加到嵌套的json对象

时间:2016-03-28 14:26:25

标签: mysql json mysql-5.7

我正在尝试处理JSON列的mysql 5.7的新API。我的test列如下所示:

{"foo":{"efg":1}, "bar":{"abc":0}}

我想要做的是附加其中一个键,例如foo,这样会产生"foo":{"efg":1, "klm":2}。到目前为止我在their documentation之后尝试过的事情:

mysql> select json_insert(test, '$.foo', 10, '$.foo.klm', 2) from table1
       where name='Joe';

它的作用是替换"efg":1,结果是"foo":{"klm":2}

mysql> select json_array_append(test, '$.foo', '{"klm":2}') from table1 where
       name="Joe';

上述行显然会将foo转换为数组"foo":[{"efg":1}, {"klm":2}],这不是我想要的。

我尝试将查询组合在一起:

mysql> select json_insert(test, '$.foo', 10, '$.foo', 
       select json_merge(select json_extract(test, '$.foo') from table1 
       where name="Joe"), '{"klm":2}') from table1 where name="Joe";

这只是给我一个语法错误near select json_extract(test, '$.foo')

非常感谢任何建议。

1 个答案:

答案 0 :(得分:4)

我无法重现这个问题。

测试:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.11    |
+-----------+
1 row in set (0.00 sec)

mysql> SET @`test` := '{"foo": {"efg":1}, "bar": {"abc":0}}';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT JSON_INSERT(@`test`,/*'$.foo', 10,*/ '$.foo.klm', 2);
+--------------------------------------------------+
| JSON_INSERT(@`test`, '$.foo.klm', 2)             |
+--------------------------------------------------+
| {"bar": {"abc": 0}, "foo": {"efg": 1, "klm": 2}} |
+--------------------------------------------------+
1 row in set (0.00 sec)

<强>更新

mysql> DROP TABLE IF EXISTS `table1`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `table1` (
    -> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> `test` JSON
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `table1`
    ->     (`test`)
    -> VALUES
    ->     ('{"foo": {"efg":1}, "bar": {"abc":0}}');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT `id`, `test` FROM `table1`;
+----+----------------------------------------+
| id | test                                   |
+----+----------------------------------------+
|  1 | {"bar": {"abc": 0}, "foo": {"efg": 1}} |
+----+----------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT JSON_INSERT(@`test`, '$.foo.klm', 2)
    -> FROM `table1`
    -> WHERE `id` = 1;
+--------------------------------------------------+
| JSON_INSERT(@`test`, '$.foo.klm', 2)             |
+--------------------------------------------------+
| {"bar": {"abc": 0}, "foo": {"efg": 1, "klm": 2}} |
+--------------------------------------------------+
1 row in set (0.00 sec)

mysql> UPDATE `table1`
    -> SET `test` = JSON_INSERT(@`test`, '$.foo.klm', 2)
    -> WHERE `id` = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT `id`, `test` FROM `table1`;
+----+--------------------------------------------------+
| id | test                                             |
+----+--------------------------------------------------+
|  1 | {"bar": {"abc": 0}, "foo": {"efg": 1, "klm": 2}} |
+----+--------------------------------------------------+
1 row in set (0.00 sec)