如何编写Postgres JSONB子句

问题描述 投票:0回答:1

我有下表:

CREATE TABLE mbiz.dictionary_groups (
    slgr_id jsonb NOT NULL,
    stored jsonb NOT NULL,
    modified_date timestamp NOT NULL DEFAULT now(),
    CONSTRAINT dictionary_groups_pkey PRIMARY KEY (slgr_id)
);

并将json对象保留在名为“存储的”列中,例如:

{
    "Position": 
        {"RotationId": 0, "SubGroupId": 0, "DiscoutGroupId": 99, "PriceIntervalId": 0},
    "DefaultValue": 0.0, 
    "PositionValues": 
    [
        {"Value": 26.0, "ProfileId": 1}, 
        {"Value": 18.0, "ProfileId": 2}, 
        {"Value": 33.0, "ProfileId": 12}
    ]
}

我正在尝试查找所有记录,其中“ PositionValues”中的任何记录的“ ProfileId”都等于2。

这是Postgres 9.5,我发现了一些提示,建议用户使用?@>,但尝试时会收到错误消息:

“ SQL错误[42883]:错误:运算符不存在:@>未知提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。位置:85“

postgresql jsonb postgresql-9.5
1个回答
0
投票

您需要在右侧提供一个数组:

select *
from dictionary_groups
where "stored" -> 'PositionValues' @> '[{"ProfileId": 2}]';

Online example

© www.soinside.com 2019 - 2024. All rights reserved.