查询仅提取在 jsonb 列中包含一组 'values' 的记录
Query to extract only records which contains a set of 'values' in jsonb column
我有 table,它有一个名为 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
现在我有一组要搜索的值(不是键,因为我事先没有 have/know 键)像这样:
["IN", "Online"]
并且这两个都需要出现在记录标签的值中。
我参考并尝试了很多东西但都失败了
转介:How to filter a value of any key of json in postgres
我应该如何为这个用例编写查询?
假设您的搜索词在 jsonb
数组中,将其转换为 text[]
,将 tags
列中的值转换为 text[]
,然后使用 @>
包含运算符:
with search_terms as (
select array_agg(el) as search_array
from jsonb_array_elements_text('["IN", "Online"]') as et(el)
)
select m.id, m.tags
from search_terms s
cross join mytable m
cross join lateral jsonb_each_text(tags) t(k,v)
group by m.id, m.tags, s.search_array
having array_agg(t.v) @> s.search_array
;
我有 table,它有一个名为 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
现在我有一组要搜索的值(不是键,因为我事先没有 have/know 键)像这样:
["IN", "Online"]
并且这两个都需要出现在记录标签的值中。
我参考并尝试了很多东西但都失败了
转介:How to filter a value of any key of json in postgres
我应该如何为这个用例编写查询?
假设您的搜索词在 jsonb
数组中,将其转换为 text[]
,将 tags
列中的值转换为 text[]
,然后使用 @>
包含运算符:
with search_terms as (
select array_agg(el) as search_array
from jsonb_array_elements_text('["IN", "Online"]') as et(el)
)
select m.id, m.tags
from search_terms s
cross join mytable m
cross join lateral jsonb_each_text(tags) t(k,v)
group by m.id, m.tags, s.search_array
having array_agg(t.v) @> s.search_array
;