【发布时间】:2018-12-21 08:37:45
【问题描述】:
这是名为“debts”的数据库列中的 JSON 数据:
{
"debt_block_1":{
"debt_type_1":"House",
"repayment_frequency_1":"Monthly",
"financial_institution_1":"anz",
"repayment_amount_1":"899",
"amount_your_share_1":"98",
"loan_term_1":"3",
"rate_1":"3"
},
"debt_block_2":{
"debt_type_2":"Creditcard",
"repayment_frequency_2":"Monthly",
"financial_institution_2":"anz",
"credit_limit_2":"2000",
"remaining_balance_2":"200"
},
"special_notes":""
}
在这里,如果金融机构是“anz”,我需要获取债务类型。我没有为 JSON 数据类型编写 SQL 查询的经验。
我尝试了以下查询:
select JSON_SEARCH(debts, 'all' , 'anz') from table_name;
查询结果如下:
["$.debt_block_1.financial_institution_1", "$.debt...
编辑开始
我执行的另一个查询是
SELECT debts, JSON_EXTRACT(debts, "$.debt_type")
FROM application_data
WHERE JSON_EXTRACT(debts, "$.financial_institution") = 'anz'
没有结果。
编辑结束
关于如何获得debt_type 值的任何建议,其中financial_institution ="anz"?
【问题讨论】:
-
我建议您在 SQL 之前先修改 json 文档.....例如添加另一个键“debt_type”和“financial_institution”并复制每个段的值。
-
下面的dbfiddle可以给你一些想法。该示例在 MariaDB 10.2.12 中,但 MySQL >= 8.0.4 应该可以工作。
-
@wchiquito 感谢您抽出宝贵时间,只是想知道查询中 '^\[|\]$ 的目的是什么,
-
基本上为
JSON_EXTRACT函数创建正确的语法。 -
有没有办法为 mysql 5.7 @wchiquito 编写它