postgresql 中的通配符搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17160611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wildcard search in postgresql
提问by Craig Ringer
In postgresql, I have mangaged to add wildcard pattern (*)to the query using SIMILAR TO option. So my query will be,
在 postgresql 中,我设法使用 SIMILAR TO 选项将通配符模式 (*)添加到查询中。所以我的查询将是,
SELECT * FROM table WHERE columnName SIMILAR TO 'R*'
This query would return all entities starting from 'R' and not 'r'. I want to make it case insensitive.
此查询将返回从 'R' 而不是 'r' 开始的所有实体。我想让它不区分大小写。
回答by Craig Ringer
Use ILIKE
:
使用ILIKE
:
SELECT * FROM table WHERE columnName ILIKE 'R%';
or a case-insensitive regular expression:
或不区分大小写的正则表达式:
SELECT * FROM table WHERE columnName ~* '^R.*';
Both are PostgreSQL extensions. Sanjaya has already outlined the standards-compliant approaches - filtering both sides with lower(...)
or using a two-branch SIMILAR TO
expression.
两者都是 PostgreSQL 扩展。Sanjaya 已经概述了符合标准的方法 - 使用lower(...)
或使用双分支SIMILAR TO
表达式过滤双方。
SIMILAR TO
is less than lovely and best avoided. See this earlier answer.
SIMILAR TO
不那么可爱,最好避免。请参阅此较早的答案。
You could write:
你可以写:
SELECT * FROM table WHERE columnName SIMILAR TO '(R|r)%'
but I don't particularly recommend using SIMILAR TO
.
但我并不特别推荐使用SIMILAR TO
.
回答by Sanjaya Liyanage
try
尝试
SELECT * FROM table WHERE columnName SIMILAR TO 'R%|r%'