1

right now I have a keyword array like: ['key1', 'key2', 'key3'.......] , the keyword can be number or character.

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?

For example: I got a table which has a column called name and a column called description

I need find all record that either name or description contains any keywords in that array.

thanks

4

2 回答 2

4

也许这个例子会有用:

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 is hello
hello
this

这里的另一个例子:

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

~* 不区分大小写,~ 区分大小写

你可以在这里试试这个例子。

于 2013-07-17T20:13:08.243 回答
2
select *
from t
where
    array[name] <@ my_array
    or array[description] <@ my_array

like运算符与any子查询表达式结合起来:

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

或者数组语法:

where name like any (array['%John%', '%Mary%'])
于 2013-07-17T19:25:48.390 回答