2016-03-23 29 views
6

Yeni MySQL JSON desteğiyle bir yönetici alanı için notlar/yorum sistemi oluşturmaya çalışıyorum. Yorumlar düzenlenebilir olmalı ve gelecekte başka şeyler için destek eklemek istedim, belki dosya ekleri (dosya yolunu JSON'da depolayamazdım, sadece dosyanın kendisi değil!). MySQL 5.7.8 JSON yeni verileri birleştirme

{ 
    "comments": [ 
    { 
     "comment": "This is a comment", 
     "user_id": 5, 
     "datecreated": "2016-03-19" 
    }, 
    { 
     "comment": "This is a comment", 
     "user_id": 1, 
     "datecreated": "2016-03-19" 
     "comments": [ 
     { 
      "comment": "This is a sub-comment", 
      "user_id": 4, 
      "datecreated": "2016-03-19" 
     }, 
     { 
      "comment": "This is a sub-comment", 
      "user_id": 4, 
      "datecreated": "2016-03-19" 
     } 
     ] 
    } 
    ] 
} 

Belirli bir tuşunu her zaman hedef gerek kalmadan array_merge benzer yeni veri() içinde birleştirmek için bir yol var olacağını düşündük.

Bu sorgu çalışır, ancak yalnızca bir şeyi, yorumun metin içeriğini hedefler. Etiketler, resim veya dosya ekleri vb. Eklemek/düzenlemek istesem çok uzun bir sorguya veya birkaç soruna ihtiyacım olurdu.

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1].comment", "This is a test comment") WHERE note_id = :note_id 

Ben JSON_OBJECT ile JSON_REPLACE ve JSON_SET işlevleri kullanarak denedik ama, user_id anlamına gelir belirtilen datecreated ve alt yorumlar üzerine yazılır olsun olmayan tüm anahtarları üzerine yazar.

UPDATE shared_notes SET json = JSON_REPLACE(json, "$.comments[1]", JSON_OBJECT("comment", "This is a test comment")) WHERE note_id = :note_id 

neredeyse eserler bir sorgunun Bu Frankenstein ama aslında eskisinin ucuna güncellenen yorumunu birleştirir:

UPDATE shared_notes SET json = JSON_SET(json, "$.comments[1]", JSON_MERGE(JSON_EXTRACT(json, "$.comments[1]"), CAST('{"comment":"Test"}' AS JSON))) WHERE note_id = :note_id 

Yani kolayca/dinamik kullanarak JSON güncelleştirmek için daha iyi bir yol yoktur MySQL veya $.comments[1].comment, $.comments[1][0].user_id vb. Tek hedef mi?

+0

Ah dostum, ben sadece bir ağrı hissedilebilir. Böyle bir fonksiyonun sağlanan json fonksiyonlarının bir parçası olmadığını anlayamıyorum. Bu çok temel bir ihtiyaç! – EscapeNetscape

cevap

0

Bu çok geç cevabı, ama yine de - böyle yapabilirsiniz:

create table sampl_test(id int, comments json); 
insert into sampl_test values(1, 
'{ 
    "comments": [ 
     { 
      "comment": "This is a comment", 
      "user_id": 5, 
      "datecreated": "2016-03-19" 
     }, 
     { 
      "comment": "This is a comment", 
      "user_id": 1, 
      "datecreated": "2016-03-19", 
      "comments": [ 
       { 
        "comment": "This is a sub-comment", 
        "user_id": 4, 
        "datecreated": "2016-03-19" 
       }, 
       { 
        "comment": "This is a sub-comment", 
        "user_id": 4, 
        "datecreated": "2016-03-19" 
       } 
      ] 
     } 
    ] 
} 
') 
; 

select json_merge('{"comments" : {"comment" : "This is a test comment" }}', comments) 

from sampl_test;