2016-03-28 81 views
1

我正在尝试用于处理JSON列的mysql 5.7的新API。我test列如下:mysql 5.7将键/值附加到嵌套的json对象

{"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')

任何意见将不胜感激。

+0

@JoachimIsaksson期望的结果应该是'{ “foo” 的:{ “EFG”:1, “KLM”:2},“栏 “:{” ABC“:0}}'。他们的文档主要用于修改数组,但我想将所有内容都保存为Object。 – denikov

+0

我使用'json_insert'得到了确切的结果,(或者只是'从table1中选择json_insert(test,'$ .foo.klm',2),其中name ='Joe';''“efg”:1'不会 –

+0

@JoachimIsaksson我不知道问题出在哪儿,我发出了一些参数,就像在你的评论中一样,它仍然替换了''foo''Object的所有内容。 – denikov

回答

2

我无法重现该问题。

测试:

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) 

UPDATE

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) 
+0

当我从你的例子逐字执行时,它工作正常。所以最初可能最初将对象错误地添加到行中?我直接在'insert into table1(test)values('{“foo”:{“efg”:1},“bar”:{“abc”:0}}'';')中加入它。这是问题吗? – denikov

+0

@denikov:更新了答案。 – wchiquito

+0

感谢您的更新。我会在几个小时内尝试它,并希望弄清楚。感谢您的帮助。 – denikov