2017-05-12 55 views
-1

的記錄在我的MySQL數據庫我存儲等的值:Mysql的JSON數據類型:要替換的多個陣列JSON值即更新嵌套JSON

Table name : access_record 
id | access 
1 | [{"camp_id":"1","permission":"r,w","read_unread_status":"0"}, 
     {"camp_id":"2","permission":"r,w","read_unread_status":"0"}] 

我想更新其具有記錄id = 1 camp_id = 2 & read_unread_status = 1的更新值

請幫我找到解決辦法。

i was try like below which is select query (testing): 
SELECT id,(SELECT json_search(access->'$[*].camp_id','all',2)) FROM access_record WHERE json_search(access->'$[*].camp_id','all',2) IS NOT NULL AND id = 1 

它返回嵌套陣列即{ 「camp_id」: 「2」, 「許可」: 「R,W」, 「read_unread_status」: 「0」}的id &陣列位置這個陣列的位置。但我想更新該藥水json數組的記錄...

回答

0

不完全確定,但試試這個,我會在簡單的json上工作,但是我沒有在數組上嘗試它。

UPDATE access_record SET access =JSON_SET(access, '$.read_unread_status', 1) WHERE id=1 and JSON_EXTRACT(access, '$.camp_id') = 2; 
+0

謝謝你的回放....但我的訪問記錄json是數組json,即2d json格式的關聯數組... – pAsh