mysql - 在 mysql json 列中搜索以检查数组同一索引上的多个条件

标签 mysql sql arrays json

我正在使用 MYSql 服务器 8.0.17。

我想从 security 列中获取 uId= 'UR000001' 以及 VIEW = 'Y' 的记录(显示在表)。

Viewid          Security
VW0000000002    {"security": [{"uId": "UR000001", "edit": "N", "view": "Y"}, {"uId": "UR000002", "edit": "N", "view": "Y"}]}
VW0000000013    {"security": [{"uId": "UR000001", "edit": "N", "view": "N"}, {"uId": "UR000002", "edit": "N", "view": "Y"}]}
VW0000000014    {"security": [{"uId": "UR000001", "edit": "N", "view": "Y"}, {"uId": "UR000002", "edit": "N", "view": "Y"}]}

JSON_SEARCH 函数搜索我不想要的记录的所有数组元素。

这是我尝试过的查询,但它返回所有匹配的结果 (uID='UR000001' OR View='Y')

SELECT viewid, 
       Json_search(`security`, 'one', 'UR000001', NULL, '$.security[*].uId'), 
       Json_search(`security`, 'one', 'Y', NULL, '$.security[*].view') 
FROM   vw_viewmaster 
WHERE  Json_search(`security`, 'one', 'UR000001', NULL, '$.security[*].uId') 
       AND Json_search(`security`, 'one', 'Y', NULL, '$.security[*].view');

实际结果:(uID='UR000001' OR View='Y')

VW0000000002    "$.security[0].uId" "$.security[0].view"
VW0000000013    "$.security[0].uId" "$.security[1].view"
VW0000000014    "$.security[0].uId" "$.security[0].view"

预期结果:(uID='UR000001' AND View='Y')

VW0000000002    "$.security[0].uId" "$.security[0].view"
VW0000000014    "$.security[0].uId" "$.security[0].view"

最佳答案

在 MySQL 8.0 中,您可以使用 handy JSON function json_table()将 json 数组转换为行。然后您可以搜索结果集。

以下查询提供所有 viewid,其至少一个属性 uId 的数组元素等于 'UR000001' 且属性 View 'Y':

select v.viewid
from vw_viewmaster v
where exists (
    select 1
    from json_table(
        v.security -> '$.security',
        '$[*]'
        columns(
            uid  varchar(50) path '$.uId',
            edit varchar(1)  path '$.edit',
            view varchar(1)  path '$.view'
        )
    ) x
    where x.uid = 'UR000001' and x.view = 'Y'
);

对于您的数据集,this produces :

| viewid       |
| ------------ |
| VW0000000002 |
| VW0000000014 |

如果您想要匹配数组对象的详细信息,则:

select v.viewid, x.*
from vw_viewmaster v
cross join json_table(
    v.security -> '$.security',
    '$[*]'
    columns(
        rowid for ordinality,
        uid   varchar(50) path '$.uId',
        edit  varchar(1)  path '$.edit',
        view  varchar(1)  path '$.view'
    )
) x
where x.uid = 'UR000001' and x.view = 'Y'

作为奖励,rowid 为您提供 JSON 数组中匹配对象的索引(第一个对象的索引为 1)。

This yields :

| viewid       | rowid | uid      | edit | view |
| ------------ | ----- | -------- | ---- | ---- |
| VW0000000002 | 1     | UR000001 | N    | Y    |
| VW0000000014 | 1     | UR000001 | N    | Y    |

但是请注意,如果数组中存在多个满足条件的对象,则上述查询将在原始表中每行生成多行(这就是我在中使用 exists 的原因)第一个查询)。

关于mysql - 在 mysql json 列中搜索以检查数组同一索引上的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61100901/

相关文章:

php - 从数据库存储和检索电子邮件正文时,在引号前加反斜杠。

mysql - 插入到两个表

mysql - 从 SQL 表中删除与其他表内有连接的条目

sqlite:有没有办法在 SELECT 子句中进行条件 OR/COALESCE?

sql - Entity Framework Core - 手动将 id 分配给链接的实体

javascript - 通过元素的子字符串环回过滤数组中的对象

javascript - Vanilla JS,尝试访问数组中的对象

php - Magento,帮助 SQL 查询获取完整的产品 URL

php - 如何在 yii 中将 mysql 查询转换为 CDbCriteria?

php - 从 PHP 数组中删除选定单词的有效方法