gpt4 book ai didi

mysql - 如何搜索JSON数据关联数组mysql 5.5

转载 作者:行者123 更新时间:2023-11-29 06:28:47 25 4
gpt4 key购买 nike

我在MySQL 5.5中的表的列中存储了JSON关联数组数据。我想在 JSON 中搜索具有键和值的一个特定数组键我的 JSON 结构:

   {
"Interface_1":{
"Caption":"[00000004] Intel(R) Dual Band Wireless-AC 3160",
"MACAddress":"D0:7E:35:D3:C1:52",
"DHCPServer":"192.168.1.1",
"IPAddress":"192.168.1.101",
"DNSServerSearchOrder":[
"8.8.8.8",
"9.9.9.9"
],
"DefaultIPGateway":"192.168.1.1",
"IPSubnet":"255.255.255.0"
},
"Interface_2":{
"Caption":"[00000011] VMware Virtual Ethernet Adapter for VMnet1",
"MACAddress":"00:50:56:C0:00:01",
"IPAddress":[
"192.168.29.1",
"fe80::d9f4:2bfa:59f:b9ba"
],
"IPSubnet":[
"255.255.255.0",
"64"
]
},
"Interface_3":{
"Caption":"[00000013] VMware Virtual Ethernet Adapter for VMnet8",
"MACAddress":"00:50:56:C0:00:08",
"IPAddress":[
"192.168.71.1",
"fe80::acec:db96:af3a:4680"
],
"IPSubnet":[
"255.255.255.0",
"64"
]
}
}

因此,对于按键值搜索,我有以下查询:

SELECT * FROM `extable` 
WHERE network RLIKE '"MACAddress":"[[:<:]]00:50:56:C0:00:08[[:>:]]"'

它工作正常,但我的问题是我无法选择 Interface_1 并在其上搜索 MACAddress。我想选择 Interface_1 并在其上搜索 MACAddress

最佳答案

您可以将 Interface_1 对象键包含到正则表达式中,从该点开始搜索,但不超过任何 }(对象结尾)字符来查找 MAC 地址:

SELECT * FROM `extable` 
WHERE network RLIKE '"Interface_1":{[^}]*"MACAddress":"[[:<:]]00:50:56:C0:00:08[[:>:]]"'

Demo on dbfiddle

关于mysql - 如何搜索JSON数据关联数组mysql 5.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755142/

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