繁体   English   中英

如何在sql(postgresql)中的json字段中搜索值

[英]how to search values in json field in sql (postgresql)

假设我有一个名为关键字的列,其值如['wood', 'grass', 'tree', 'plant'] ,现在我如何查询该数组是否包含woodgrassboth of them

我的解决方案(这里我使用文本字符串并搜索它)

  select * from table where keywords_column ~* 'wood';

但仅限于一个词。 如果我能在 knex.js 或 Adonis Lucid 中获得解决方案,那就太好了

我认为这可能有效,但我没有测试它:

select * from table where keywords_column ~* 'wood' or keywords_column ~* 'grass';

您可以使用包含运算符?|

select * 
from the_table 
where keywords_column ?| array['wood', 'grass'];

这假定keywords_columns被定义为jsonb (如果您存储JSON 值,它应该是)。 如果不是,则需要将其强制转换为keywords_column::jsonb

在线示例

我会这样做:

SELECT * from table WHERE column SIMILAR TO '(wood |grass |tree |plant )%';

暂无
暂无

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

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