如何编写 Postgres JSONB Where 子句
How to write Postgres JSONB Where clausule
我有以下 Table:
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)
);
并在名为 'stored' 的列中保留 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 Error [42883]: ERROR: operator does not exist: @> unknown
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 85"
您需要在右侧提供一个数组:
select *
from dictionary_groups
where "stored" -> 'PositionValues' @> '[{"ProfileId": 2}]';
我有以下 Table:
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)
);
并在名为 'stored' 的列中保留 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 Error [42883]: ERROR: operator does not exist: @> unknown
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 85"
您需要在右侧提供一个数组:
select *
from dictionary_groups
where "stored" -> 'PositionValues' @> '[{"ProfileId": 2}]';