在数组里面模糊查询某个内容


搜索:张三  ,把叫:tags: [“张三”,"abc"]和 tags: ["张三风","万立"] 的都会找出来。

这里用了个折中的办法:把数组组合成字符串,再来模糊搜索

Topic.select(:tags).where('concat(tags) ILIKE ?', "%b%")

其它参考
========
SiteTopic.where('? =ANY(tags)', "abc")



参考这个例子:
select *
from people
where name like any (
    select concat(first_name, last_name, '%')
    from other_people
)

PostgreSQL also supports full POSIX regular expressions:

select * from table where value ~* 'foo|bar|baz';

The ~* is for a case insensitive match, ~ is case sensitive.

Another option is to use ANY:

select * from table where value  like any (array['%foo%', '%bar%', '%baz%']);
select * from table where value ilike any (array['%foo%', '%bar%', '%baz%']);

You can use ANY with any operator that yields a boolean. I suspect that the regex options would be quicker but ANY is a useful tool to have in your toolbox.


阅读量: 101
发布于:
修改于: