MySQL/MariaDB JSON_EXTRACT 和 JSON_CONTAINS

标签 mysql json mariadb mysql-json

我有一个具有以下结构的表:

select * from test_table;

id |load_balancer_name |listener_descriptions                                                                                                                                                                                       |
---|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1  |with_cert_1        |[{"Listener": {"Protocol": "HTTPS", "LoadBalancerPort": 443, "InstanceProtocol": "HTTP", "InstancePort": 9005, "SSLCertificateId": "arn:aws:acm:us-west-2:xxxx:certificate/xxx"}, "PolicyNames": ["xxxx"]}] |
2  |with_cert_1        |[{"Listener": {"Protocol": "HTTPS", "LoadBalancerPort": 443, "InstanceProtocol": "HTTP", "InstancePort": 9005, "SSLCertificateId": "arn:aws:acm:us-west-2:xxxx:certificate/xxx"}, "PolicyNames": ["xxxx"]}] |
3  |with_cert_2        |[{"Listener": {"Protocol": "HTTPS", "LoadBalancerPort": 443, "InstanceProtocol": "HTTP", "InstancePort": 9005, "SSLCertificateId": "arn:aws:acm:us-west-2:xxxx:certificate/yyy"}, "PolicyNames": ["xxxx"]}] |
4  |no_cert            |                                                                                                                                                                                                            |

我需要的是根据 listener_descriptions 列进行一些搜索。为了确保 JSON_* 方法有效,我 做了这个查询,效果很好:

select 
id, load_balancer_name,
JSON_EXTRACT(listener_descriptions, "$[*].Listener.SSLCertificateId")
from test_table;

id |load_balancer_name |JSON_EXTRACT(listener_descriptions, "$[*].Listener.SSLCertificateId") |
---|-------------------|----------------------------------------------------------------------|
1  |with_cert_1        |["arn:aws:acm:us-west-2:xxxx:certificate/xxx"]                        |
2  |with_cert_1        |["arn:aws:acm:us-west-2:xxxx:certificate/xxx"]                        |
3  |with_cert_2        |["arn:aws:acm:us-west-2:xxxx:certificate/yyy"]                        |
4  |no_cert            |                                                                      |

现在我想选择匹配 SSLCertificateId 的所有行:

select 
*
from test_table
where JSON_CONTAINS(listener_descriptions, '"arn:aws:acm:us-west-2:xxxx:certificate/xxx"', "$[*].Listener.SSLCertificateId")
;

但是没有找到结果。我尝试在 JSON_CONTAINS 的第二个参数中使用单引号和双引号的多种组合,但没有成功。

version()                                |
-----------------------------------------|
10.3.8-MariaDB-1:10.3.8+maria~bionic-log |

最佳答案

JSON_CONTAINS() 不允许在其路径中使用 [*]。相反,使用 JSON_EXTRACT() 提取所有证书的数组,并在其上使用 JSON_CONTAINS()

select *
FROM test_table
WHERE JSON_CONTAINS(JSON_EXTRACT(listener_descriptions, "$[*].Listener.SSLCertificateId"), '"arn:aws:acm:us-west-2:xxxx:certificate/xxx"')
;

DEMO

关于MySQL/MariaDB JSON_EXTRACT 和 JSON_CONTAINS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52083608/

相关文章:

php - 将站点移至新主机时出现问题

mysql - 关于MySQL中类型的问题

python - 将 cURL 转换为 Python 请求错误

php - 为什么用php创建的excel文件与浏览器中的表格不一样?

php - 找不到语法中的错误

python - 将 JSON 字符串转换为 Python 字典

mysql - 通过 sequelpro 连接到 Mariadb 到 synology NAS

mysql - 将默认列值从日期时间转换为 varchar

mysql - 如何使用联合表从多个记录中选择不同的记录

mysql - 硬编码值工作正常,但是当从参数传递时,在 i-Report 中不会给出相同的结果