在对象数组中的属性值中选择行,多个值之一
我的数据如下:Postgres
ID | 数据 |
---|---|
1 | [{“ A”:3,“ B”:“ green”} 的JSONB列中的对象数组}] |
2 | [{{“ a”:3,“ b”:“ red”},{“ a”:5,“ b”:“ yellow”}] |
3 | [{“ a”:3,“ b”:“” {” |
橙色“} , 代码>“黄色”
我知道我可以使用JSONB_ARRAY_ELEMENTS
获取所有b
值,
select jsonb_array_elements(data) ->> 'b' from table
但我未能在此处查询中使用该数据此
select * from table where jsonb_array_elements(data) ->> 'b' && ARRAY["green","yellow"]::varchar[]
(不允许在哪里允许使用“集合返回功能”)
I have data shaped like this: arrays of objects in a jsonb column in postgres
id | data |
---|---|
1 | [{"a":3, "b":"green"} ,{"a":5, "b":"blue"}] |
2 | [{"a":3, "b":"red"} ,{"a":5, "b":"yellow"}] |
3 | [{"a":3, "b":"orange"} ,{"a":5, "b":"blue"}] |
I am trying to select the rows where b
is either "green"
or "yellow"
I know I can unroll the data using jsonb_array_elements
to get all the b
values
select jsonb_array_elements(data) ->> 'b' from table
but I am failing to use that in a where query like this
select * from table where jsonb_array_elements(data) ->> 'b' && ARRAY["green","yellow"]::varchar[]
(not working "set-returning functions are not allowed in WHERE")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
@>
运算符或JSON路径表达式:
或通过不存在存在条件的数组:
You can use the
@>
operatorOr a JSON path expression:
Or by unnesting the array with an EXISTS condition:
您可以尝试使用列名称和
的子查询,
如下sqlfiddle
note
array
滤波器值需要使用单引号而不是双引号You can try to use subquery with a column alias name and
ANY
like belowsqlfiddle
NOTE
ARRAY
filter value need to use single quote instead of double quote