小编Cra*_*ing的帖子

PostgreSQL,在数字 JSON 数组中按值查找元素

我有一个表定义为:

create table dummy (jdata jsonb);
Run Code Online (Sandbox Code Playgroud)

我插入了以下两行:

insert into dummy values ('["dog","cat","elephant","waffle"]');
insert into dummy values ('[1,2,3,4]');
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 jsonb?&运算符,它可以让您提出问题“所有这些键/元素字符串都存在吗?”

使用字符串字段的示例有效:

select * from dummy where jdata ?& array['cat','dog'];
            jdata                 
--------------------------------------
["dog", "cat", "elephant", "waffle"]
(1 row)
Run Code Online (Sandbox Code Playgroud)

但是,尝试使用包含数字的数组来执行此操作是行不通的:

select * from dummy where jdata ?& array['1','2'];
  jdata 
  -------
(0 rows)

select * from dummy where jdata ?& array['1','2'];
 jdata 
 -------
 (0 rows)

select * from dummy where jdata ?& array[1,2];
ERROR:  operator does not exist: jsonb ?& integer[]
LINE …
Run Code Online (Sandbox Code Playgroud)

postgresql array json

6
推荐指数
1
解决办法
4195
查看次数

标签 统计

array ×1

json ×1

postgresql ×1