查询仅提取包含一组' value&#x27的记录。在JSONB专栏中

发布于 2025-01-24 18:13:17 字数 1798 浏览 3 评论 0原文

我有一个名为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”]这些都需要在记录标签的值中存在。

我转介并尝试了很多事情,但失败了

如何过滤postgres中JSON的任何键的值

我应该如何编写此用例的查询?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2025-01-31 18:13:17

假设您的搜索术语在jsonb数组中,将其转换为text [],将值从tags列转换为text> text> 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
;

工作小提琴

Assuming your search terms are in a jsonb array, turn it into text[], turn the values from the tags column into text[], and use the @> contains operator:

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
;

Working Fiddle

淡淡の花香 2025-01-31 18:13:17

示例 by

select m.id, m.tags
  from mytable m
       cross join lateral 
       (SELECT array_agg(t.v) AS v
       FROM jsonb_each_text(tags) t(k,v)) AS t
WHERE t.v @> '{"IN", "Online"}'

基于示例 @mikeorganek

Example without use GROUP BY

select m.id, m.tags
  from mytable m
       cross join lateral 
       (SELECT array_agg(t.v) AS v
       FROM jsonb_each_text(tags) t(k,v)) AS t
WHERE t.v @> '{"IN", "Online"}'

Based on an example @MikeOrganek

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文