mysql 中 JSON 数据类型列中的嵌套更新查询 5.7
Nested update query in JSON data type column in mysql 5.7
我在 table 中添加了一个新的 JSON 数据类型列 (bill_plans)
。现在我想像这样更新 bill_plans
列
[{ "cycle": 1, "fee": 1000}, { "cycle": 3, "fee": 2800}, { "cycle": 10, "fee": 10000} ]
我成功创建了专栏,并且还能够更新 bill_plans 专栏。
table 包含 bill_cycle
和 fees
也作为现有列,所以我想更新 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;
但现在我无法理解如何从 table
的现有列中更新 bill_plans
MySQL 具有预定义函数来对 JSON 数组和对象执行操作。
您可以使用以下查询来获得结果。
方法一
使用通用语法
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, '"}]')
方法二
使用 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
我在 table 中添加了一个新的 JSON 数据类型列 (bill_plans)
。现在我想像这样更新 bill_plans
列
[{ "cycle": 1, "fee": 1000}, { "cycle": 3, "fee": 2800}, { "cycle": 10, "fee": 10000} ]
我成功创建了专栏,并且还能够更新 bill_plans 专栏。
table 包含 bill_cycle
和 fees
也作为现有列,所以我想更新 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;
但现在我无法理解如何从 table
的现有列中更新bill_plans
MySQL 具有预定义函数来对 JSON 数组和对象执行操作。
您可以使用以下查询来获得结果。
方法一 使用通用语法
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, '"}]')
方法二 使用 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