简体   繁体   English

如何在jsonb postgresql中查询并转换为谓词JPA

[英]How to query in jsonb postgresql and convert to predicate JPA

i have stuck :)我卡住了:)

does anyone know how query postgresql in jsonb i have table USER有谁知道如何在 jsonb 中查询 postgresql 我有表 USER

  • INT id, INT ID,
  • Varchar name, Varchar 名称,
  • jsonb categoryId jsonb 类别 ID

the example data in categoryId field like this = [1,2,3,4,5] categoryId 字段中的示例数据如下 = [1,2,3,4,5]

I have tried this query which works:我试过这个有效的查询:

select * 
from user where categoryId @> '2'::jsonb ;  

but how to query with multiple params like但是如何使用多个参数进行查询,例如

select * 
from user 
where categoryId @> '1,3,4'::jsonb

and i will implement this to hibernate jpa/jpql-predicate, but i want to know native query first我将实现它以休眠 jpa/jpql-predicate,但我想先了解本机查询

Thankyou so much非常感谢

In SQL you can use the ?|在 SQL 中你可以使用?| operator for this 运营商为此

select *
from "user"
where categoryid ?| array['1','3','4'];

That will return rows where at least one of the values is contained in the JSON array.这将返回其中至少一个值包含在 JSON 数组中的行。 If you want to find those that contain all values, use the ?& operator instead.如果要查找包含所有值的值,请改用?&运算符。


To use the @> operator you would need to use a JSON array on the right hand side:要使用@>运算符,您需要在右侧使用 JSON 数组:

select *
from "user"
where categoryid @> '[1,3,4]'::jsonb

Note that user is a reserved keyword.请注意, user是保留关键字。 You will have to enclose it in double quotes every time you want to refer to the table.每次要引用该表时,都必须将其括在双引号中。 I would highly recommend to find a different table name.我强烈建议找一个不同的表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM