簡體   English   中英

mysql 5.7中JSON數據類型列中的嵌套更新查詢

[英]Nested update query in JSON data type column in mysql 5.7

我在表中添加了新的JSON數據類型列(bill_plans) 現在,我想像這樣更新bill_plans

[{ "cycle": 1, "fee": 1000}, { "cycle": 3, "fee": 2800}, { "cycle": 10, "fee": 10000} ]

我成功創建了該列,並且還能夠更新bill_plans列。

該表包含bill_cyclefees也可以作為現有列,所以我想更新bill_plans列這樣的事情

[{ "cycle": value from the bill_cycle column, "fee": value from the fees column}, { "cycle": value from the bill_cycle column, "fee": value from the fees column}]

簡單的更新查詢是這樣的

update  coaching_class_entries set bill_plans =  ('[{"cycle": 1, "fee": 1000}]') where id = 1;

但是現在我無法理解如何從表的現有列中更新bill_plans

MySQL具有預定義的函數,可以對JSON數組和對象執行操作。

您可以使用以下查詢來實現您的結果。

方法1:使用常規語法

UPDATE coaching_class_entries     
  SET bill_plans = '[ {"cycle": 1, "fee": 1000 } ]'

在這種情況下,您可能需要根據列中的數據更新值。 您可以使用CONCAT運算符形成json字符串

UPDATE coaching_class_entries     
  SET bill_plans = CONCAT('[{"cycle":"', bill_cycle, '","fee":"', fees, '"}]')

方法2:使用JSON函數

UPDATE coaching_class_entries     
  SET bill_plans = JSON_ARRAY(JSON_OBJECT("cycle", bill_cycle, "fee", fees))

您可以在這里參考完整的文檔https://dev.mysql.com/doc/refman/5.7/en/json.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM