查询仅提取包含一组' value&#x27的记录。在JSONB专栏中
我有一个名为tags
的列,该列是JSONB类型列。
Table "public.tagged_products"
Column | Type | Modifiers
-------------+-----------------------------+--------------------------------------------------------------
id | integer | not null default nextval('tagged_products_id_seq'::regclass)
item_id | character varying(255) | not null
tags | jsonb | not null default '{}'::jsonb
create_time | timestamp(0) with time zone | not null default now()
update_time | timestamp(0) with time zone | not null default now()
active | boolean | not null default true
Indexes:
"tagged_products_pkey" PRIMARY KEY, btree (id) "uc_attributes_uid" UNIQUE CONSTRAINT, btree (tags, item_id) "lots_idx_attr" gin (tags)
"lots_idx_uid" btree (item_id)
示例数据
-[ RECORD 1 ]-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id | 17
item_id | 9846
tags | {"coo": "IN", "owner": "Online", "vastxt": "", "ecc_ibd": "180000010", "hm_order": "400000010", "entitled_to_dispose": "W141"}
create_time | 2022-02-24 11:49:23+05:30
update_time | 2022-02-24 11:49:23+05:30
active | t
现在我有一组我想搜索的值(不是键,因为我没有/不知道我的钥匙了)这样的值:
[in“ in”,“ inloine”]
这些都需要在记录标签的值中存在。
我转介并尝试了很多事情,但失败了
我应该如何编写此用例的查询?
I have table which has a column named tags
which is a jsonb type column.
Table "public.tagged_products"
Column | Type | Modifiers
-------------+-----------------------------+--------------------------------------------------------------
id | integer | not null default nextval('tagged_products_id_seq'::regclass)
item_id | character varying(255) | not null
tags | jsonb | not null default '{}'::jsonb
create_time | timestamp(0) with time zone | not null default now()
update_time | timestamp(0) with time zone | not null default now()
active | boolean | not null default true
Indexes:
"tagged_products_pkey" PRIMARY KEY, btree (id) "uc_attributes_uid" UNIQUE CONSTRAINT, btree (tags, item_id) "lots_idx_attr" gin (tags)
"lots_idx_uid" btree (item_id)
Sample Data
-[ RECORD 1 ]-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id | 17
item_id | 9846
tags | {"coo": "IN", "owner": "Online", "vastxt": "", "ecc_ibd": "180000010", "hm_order": "400000010", "entitled_to_dispose": "W141"}
create_time | 2022-02-24 11:49:23+05:30
update_time | 2022-02-24 11:49:23+05:30
active | t
Now I have a set of values which I want to search (not keys because i don't have/know the keys with me before hand) like this:
["IN", "Online"]
and both of these needs to be present in the record tags' values.
I referred and tried many things but failed
Referred : How to filter a value of any key of json in postgres
How should I go about writing the query for this use case ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的搜索术语在
jsonb
数组中,将其转换为text []
,将值从tags
列转换为text> text> text []
,并使用@>
包含操作员:工作小提琴
Assuming your search terms are in a
jsonb
array, turn it intotext[]
, turn the values from thetags
column intotext[]
, and use the@>
contains operator:Working Fiddle
示例 by
基于示例 @mikeorganek
Example without use
GROUP BY
Based on an example @MikeOrganek