[英]how to search values in json field in sql (postgresql)
lets say i have a column named keyword with values like ['wood', 'grass', 'tree', 'plant']
, now how do I query that if this array contains wood
or grass
or both of them
假设我有一个名为关键字的列,其值如['wood', 'grass', 'tree', 'plant']
,现在我如何查询该数组是否包含wood
或grass
或both of them
my solution (here I am using a text string and searching through it)我的解决方案(这里我使用文本字符串并搜索它)
select * from table where keywords_column ~* 'wood';
but it is limited to one word only.但仅限于一个词。 It would be great if I would get solution in knex.js or Adonis Lucid如果我能在 knex.js 或 Adonis Lucid 中获得解决方案,那就太好了
我认为这可能有效,但我没有测试它:
select * from table where keywords_column ~* 'wood' or keywords_column ~* 'grass';
You can use the contains operator ?|
您可以使用包含运算符?|
select *
from the_table
where keywords_column ?| array['wood', 'grass'];
This assumes that keywords_columns
is defined as jsonb
(which it should be if you store JSON values).这假定keywords_columns
被定义为jsonb
(如果您存储JSON 值,它应该是)。 If it's not, you need to cast it keywords_column::jsonb
如果不是,则需要将其强制转换为keywords_column::jsonb
I would do like this:我会这样做:
SELECT * from table WHERE column SIMILAR TO '(wood |grass |tree |plant )%'; SELECT * from table WHERE column SIMILAR TO '(wood |grass |tree |plant )%';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.