javascript alasql JSON查询-限制或用户错误?无法查询数组内的项目,但该项目不在第一个位置

l7mqbcuq  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(112)

我刚刚发现了alasql,它太棒了!但是我不确定这是一个已知的限制还是我的查询有问题,但是由于某种原因,我无法使用下面的查询在下面的JSON中找到值“algb 2b-gb”。看起来如果没有提到数组中项的确切位置,那么查询将无法给予结果。这是一个已知的限制还是有更好的方法来查询数组项?
感谢你的想法。
下面是SQL查询:

SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->0 = "algb2b-gb"

下面是我的JSON

[{
        "id": "00u1fcghdvmRWnNb81d8",
        "profile": {
            "lastName": "AUvalidation",
            "ad_site_permissions": [
                "algb2b-us",
                "algb2b-it",
                "algb2b-de",
                "algb2b-au",
                "algb2b-nz",
                "algb2b-nl",
                "algb2b-fr",
                "algb2b-gb",
                "algb2b-es",
                "algb2b-pr",
                "algb2b-ca",
                "algb2b-br",
                "algb2b-ch"
            ],
            "ad_system_permissions": [
                "bpReadAccess",
                "ecomReadAccess"
            ]

        }
    }, {
        "id": "00u2zvh77sVZMACHYLIZ",
        "profile": {
            "ad_site_permissions": [
                "algb2b-ca",
                "algb2b-fr",
                "algb2b-it",
                "algb2b-de",
                "algb2b-nl",
                "algb2b-au",
                "algb2b-nz",
                "algb2b-gb",
                "algb2b-es",
                "algb2b-br"
            ],
            "ad_system_permissions": [
                "bpReadAccess",
                "ecomReadAccess"
            ]
        }
    }]

我的输出是

[
    {
        "COUNT(*)": 0
    }
]

但是,如果我改变我的查询如下,那么它的工作:

SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->7 = "algb2b-gb"

访问github站点获取文档。

8i9zcol2

8i9zcol21#

SELECT COUNT( * ) FROM ? WHERE profile->ad_site_permissions->0 = "algb2b-gb"`

上面的查询将检查ad_site_permissions[0]数组项的计数,该数组项为algb 2b-us,并且不等于algb 2b-gb,因此计数为0。
您可以优化查询以搜索数组的所有索引,例如:

var data = [{a:[1,2,3,4]}, {a:[6,7,8,9]}]

var response = alasql(`SELECT COUNT( * ) FROM ? WHERE 6 = any(a)`,[data]);

console.log(response) // [ { 'COUNT(*)': 1 } ]

所以你可以试试这样

var res = alasql(`SELECT COUNT( * ) FROM ? WHERE  "algb2b-gb" = any(profile->ad_site_permissions) `, [data]);

console.log(res) // [ { 'COUNT(*)': 2 } ]

相关问题