查询搜索JSONB PSQL中的数组元素

发布于 2025-01-27 17:36:56 字数 432 浏览 4 评论 0原文

我有一个JSON节点,我必须在上面写一个PSQL查询,我的表格架名称(字符串),TagValues(JSONB)。示例tagvalue数据在下面给出了

name_tagstable,

 uid |       name(String)|          tagValues(jsonb)
-----+-------------------+-----------------------------
   1 |     myName        |    { "tags": ["xyz","pqr","xyp"]}  

我需要一个查询,该查询返回表上的搜索“ pq”的所有行

select * from Name_TagsTable where tagValues->tags contains %pq%  

I have a JSON node on which I have to write a PSQL query, My table schema name(String),tagValues(jsonb). Example tagValue data is given below

Name_TagsTable

 uid |       name(String)|          tagValues(jsonb)
-----+-------------------+-----------------------------
   1 |     myName        |    { "tags": ["xyz","pqr","xyp"]}  

I need a query that returns all rows for a search "pq" made on the tagValues of the table

select * from Name_TagsTable where tagValues->tags contains %pq%  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

追风人 2025-02-03 17:36:56

您可以使用喜欢运算符以及铸造JSONB

SELECT *
  FROM Name_TagsTable
 WHERE (tagValues->'tags')::TEXT LIKE '%pq%' 

You can use LIKE operator along with casting JSONB value to a string type such as

SELECT *
  FROM Name_TagsTable
 WHERE (tagValues->'tags')::TEXT LIKE '%pq%' 
寄风 2025-02-03 17:36:56

您需要解开元素,然后可以在适用类似条件的情况下使用它。

select nt.*
from name_tagstable nt
where exists (select *
              from jsonb_array_elements_text(tagvalue -> 'tags') as a(tag)
              where a.tag like '%pg%');

You need to unnest the elements, then you can use it in a WHERE condition that applies a LIKE condition.

select nt.*
from name_tagstable nt
where exists (select *
              from jsonb_array_elements_text(tagvalue -> 'tags') as a(tag)
              where a.tag like '%pg%');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文