gpt4 book ai didi

mysql - 如何在mysql中搜索json数据?

转载 作者:行者123 更新时间:2023-11-29 17:47:08 24 4
gpt4 key购买 nike

我有描述字段。该字段为json

描述字段的内容如下:

{
"address":
{
"street": "street 123",
"information":
{
"province": "province 123",
"regency": "regency 123",
"district": "district 123"
}
},
"mobileNumber": "123"
}

我想在json数据中搜索省份

我尝试这样:

SELECT * FROM orders WHERE LOWER(JSON_EXTRACT(description, "$.address.information.province")) LIKE "%prov%"

结果为空

如何解决这个问题?

最佳答案

问题是您的 JSON 无效。

后面不能有逗号
"district": "district 123", 

"mobileNumber": "123",

如果您尝试使用有效的 JSON,您将得到预期的结果:

select json_extract('{ "address": { "street": "street 123",         "information": { "province": "province 123",  "regency": "regency 123",             "district": "district 123" } }, "mobileNumber": "123"}', '$.address.information.province')

"province 123"

我创建了一个db-fiddle这似乎适合您的数据:

create table x (id int, description json);
insert into x values(1, '{ "address": { "street": "street 123", "information": { "province": "province 123", "regency": "regency 123", "district": "district 123" } }, "mobileNumber": "123"}');
select * from x where json_extract(description, '$.address.information.province') like '%prov%'

输出

id  description
1 {"address": {"street": "street 123", "information": {"regency": "regency 123", "district": "district 123", "province": "province 123"}}, "mobileNumber": "123"}

关于mysql - 如何在mysql中搜索json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49715125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com