简体   繁体   English

用一组关键字在Postgres中搜索的最佳方法

[英]Best way to search in postgres by a group of keyword

right now I have a keyword array like: ['key1', 'key2', 'key3'.......] , the keyword can be number or character. 现在我有一个关键字数组,例如:['key1','key2','key3'.......],关键字可以是数字或字符。

If I want to search in my table (postgres database), and find out all record contain any of keyword in that array, how can I do that? 如果我想在表(postgres数据库)中搜索,并找出包含该数组中任何关键字的所有记录,该怎么办?

For example: I got a table which has a column called name and a column called description 例如:我得到一个表,该表有一个名为name的列和一个名为description的列

I need find all record that either name or description contains any keywords in that array. 我需要找到名称或描述包含该数组中任何关键字的所有记录。

thanks 谢谢

Maybe this example will be useful: 也许这个例子会有用:

CREATE TABLE TEST(
  FIELD_KEY TEXT);

INSERT INTO TEST VALUES('this is hello');
INSERT INTO TEST VALUES('hello');
INSERT INTO TEST VALUES('this');
INSERT INTO TEST VALUES('other message');

SELECT *
FROM TEST
WHERE FIELD_KEY LIKE ANY (array['%this%', '%hel%']);

This will return: 这将返回:

this is hello
hello
this

Here other example: 这里是另一个例子:

SELECT *
FROM TEST
WHERE FIELD_KEY ~* 'this|HEL';

~* is case insensitive, ~ is case sensitive 〜*不区分大小写,〜区分大小写

You can try this example here . 您可以在此处尝试此示例。

select *
from t
where
    array[name] <@ my_array
    or array[description] <@ my_array

Couple the like operator with the any subquery expression: like运算符与any子查询表达式耦合:

select *
from t
where name like any (values ('%John%'), ('%Mary%'))

Or the array syntax: 或数组语法:

where name like any (array['%John%', '%Mary%'])

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

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