SQL Postgresql 包含在 where 子句中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15378216/
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
Postgresql Contains in where clause
提问by Gopinagh.R
Is there a function in postgres like contains
? that can be used in the where clause
to check , whether the string passed is contained in column?
postgres 中有这样的函数contains
吗?可以用于where clause
检查,传递的字符串是否包含在列中?
采纳答案by Chris Travers
There are a bunch of ways of solving this:
有很多方法可以解决这个问题:
Use
like
,ilike
, and/orSIMILAR TO
along with ||. To handle columns, something like:WHERE col1 ilike '%' || col2 || '%';
Use position as NPE's answer
You could also use
regexp_matches
but that is more complex.
使用
like
,ilike
, 和/或SIMILAR TO
与 || 一起使用。要处理列,例如:WHERE col1 ilike '%' || col2 || '%';
使用位置作为 NPE 的答案
您也可以使用,
regexp_matches
但这更复杂。
回答by NPE
You could use position()
for that. It returns zero if the substring is not found:
你可以用position()
它。如果未找到子字符串,则返回零:
position(col2 in col1) <> 0