gpt4 book ai didi

mysql - 通过 MySQL 中的 bool 值查找 JSON 对象路径

转载 作者:行者123 更新时间:2023-12-01 12:51:23 26 4
gpt4 key购买 nike

如何找到具有属性 "is_true": true 的对象的路径?

数据列中的 JSON:

[
{
"myData": {},
"is_true": true
},
{
"myData": {},
"is_true": false
},
{
"myData": {},
"is_true": false
}
]

失败的尝试:

SELECT JSON_SEARCH(data, "all", true) AS booleanObject FROM my_table

和...

SELECT JSON_SEARCH(data, "all", "true") AS booleanObject
SELECT JSON_SEARCH(data, "all", "%true%") AS booleanObject
SELECT JSON_SEARCH(data, "all", true) AS booleanObject
SELECT JSON_SEARCH(data, "all", 1) AS booleanObject
SELECT JSON_SEARCH(data, "all", true, NULL, "$") AS booleanObject

等等。

最佳答案

不幸的是它看起来像 JSON_SEARCH只搜索字符串值。你可以使用蛮力:

SELECT x.i, JSON_EXTRACT(data, CONCAT('$[', x.i, ']')) AS obj
FROM t
INNER JOIN (
SELECT 0 AS i UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3
) AS x ON x.i < JSON_LENGTH(t.data)
WHERE JSON_EXTRACT(data, CONCAT('$[', x.i, '].is_true')) = true

如果您使用的是 MySQL 8,那么更优雅的选择是使用 JSON_TABLE :

SELECT x.i, x.obj
FROM t
CROSS JOIN JSON_TABLE(t.data, '$[*]' COLUMNS(
i FOR ORDINALITY,
is_true BOOLEAN PATH '$.is_true',
obj JSON PATH '$'
)) AS x
WHERE x.is_true = true

Demo on db<>fiddle

关于mysql - 通过 MySQL 中的 bool 值查找 JSON 对象路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067156/

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