JSONB查询如何检查至少一个数组包含
JSONB query how to check at-least-one array containment
如何将下面的查询获取到 return 'true' 以反映我至少需要右侧数组中的一个值包含在左侧数组中?
SELECT '["fun", "movies"]'::jsonb @> '["fun", "movies","x"]'::jsonb;
上面的returns false
(因为'x'不在左边的数组中)
我试过了 ?|
operator link
SELECT '["fun", "movies"]'::jsonb ?| '["fun", "movies","x"]'::jsonb;
但是 postgres return 出错了
ERROR: operator does not exist: jsonb ?| jsonb LINE 2: SELECT
'["fun", "movies"]'::jsonb ?| '["fun", "movies","x...
HINT: No operator matches the given name and argument type(s). You might need to add
explicit type casts. SQL state: 42883 Character: 147
你需要先"convert" jsonb 数组到 postgres 数组:
t=# SELECT '["fun", "movies"]'::jsonb ?| translate('["fun", "movies","x"]'::jsonb::text,'[]','{}')::text[];
?column?
----------
t
(1 row)
如何将下面的查询获取到 return 'true' 以反映我至少需要右侧数组中的一个值包含在左侧数组中?
SELECT '["fun", "movies"]'::jsonb @> '["fun", "movies","x"]'::jsonb;
上面的returns false
(因为'x'不在左边的数组中)
我试过了 ?|
operator link
SELECT '["fun", "movies"]'::jsonb ?| '["fun", "movies","x"]'::jsonb;
但是 postgres return 出错了
ERROR: operator does not exist: jsonb ?| jsonb LINE 2: SELECT
'["fun", "movies"]'::jsonb ?| '["fun", "movies","x...HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. SQL state: 42883 Character: 147
你需要先"convert" jsonb 数组到 postgres 数组:
t=# SELECT '["fun", "movies"]'::jsonb ?| translate('["fun", "movies","x"]'::jsonb::text,'[]','{}')::text[];
?column?
----------
t
(1 row)