PostgreSQL通配符喜欢单词列表中的任何一个 [英] PostgreSQL wildcard LIKE for any of a list of words

查看:192
本文介绍了PostgreSQL通配符喜欢单词列表中的任何一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约25个单词的简单列表.我在PostgreSQL中有一个varchar字段,假设该列表为['foo', 'bar', 'baz'].我想在表中找到任何包含这些单词的行.可以,但是我想要更优雅的东西.

I have a simple list of ~25 words. I have a varchar field in PostgreSQL, let's say that list is ['foo', 'bar', 'baz']. I want to find any row in my table that has any of those words. This will work, but I'd like something more elegant.

select *
from table
where (lower(value) like '%foo%' or lower(value) like '%bar%' or lower(value) like '%baz%')

推荐答案

您可以使用Postgres的

You can use Postgres' SIMILAR TO operator which supports alternations, i.e.

select * from table where lower(value) similar to '%(foo|bar|baz)%';

这篇关于PostgreSQL通配符喜欢单词列表中的任何一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆