2016-03-28 11 views
1

Ich versuche die neue API für mysql 5.7, die JSON-Spalten behandelt. Meine test Spalte sieht wie folgt aus:mysql 5.7 Anfügen von Schlüssel/Wert an verschachteltes JSON-Objekt

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

Was ich möchte, ist einer der Schlüssel tun anhängen, zum Beispiel foo so dass es in "foo":{"efg":1, "klm":2} führen. Was ich bisher versucht folgende their documentation:

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

Was das tut, ist zu ersetzen "efg":1 und das Ergebnis ist "foo":{"klm":2}.

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

Die obige Linie wandelt offensichtlich foo in ein Array "foo":[{"efg":1}, {"klm":2}], das ist nicht das, was ich will.

Ich habe versucht, die Kombination von Abfragen zusammen:

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"; 

Das gibt mir nur einen Syntaxfehler near select json_extract(test, '$.foo').

Jeder Rat würde sehr geschätzt werden.

+0

@JoachimIsaksson das gewünschte Ergebnis sein sollte '{ "foo": { "efg": 1 " klm": 2}" bar ": {" abc ": 0}}'. Ihre Dokumentation arbeitet hauptsächlich mit dem Modifizieren von Arrays, aber ich möchte alles als Objekt behalten. – denikov

+0

Ich bekomme das genaue Ergebnis mit Ihrem 'json_insert', (oder wählen Sie einfach json_insert (test, $ .foo.klm, 2) aus table1 wo name = 'Joe';' '" efg ": 1' nicht verschwinden für mich. –

+0

@JoachimIsaksson Ich bin nicht sicher, wo das Problem dann ist. Ich habe einige Parameter, wie in Ihrem Kommentar, und es ersetzt noch alles für die "foo" 'Objekt. – denikov

Antwort

2

Ich kann das Problem nicht reproduzieren.

Test:

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

Wenn ich Wort für Wort von Ihrem Beispiel ausgeführt habe, hat es richtig funktioniert. Also vielleicht anfangs das Objekt falsch in die Zeile eingefügt? Ich habe es direkt in 'into table1 (test) -Werte eingefügt ('{" foo ": {" efg ": 1}," bar ": {" abc ": 0}}');'. Ist das das Problem? – denikov

+0

@denikov: Aktualisierte Antwort. – wchiquito

+0

Danke für das Update. Ich werde es in ein paar Stunden versuchen und hoffentlich es herausfinden. Danke fürs Helfen. – denikov