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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:13:25  来源:igfitidea点击:

Postgresql Contains in where clause

sqlpostgresqlselectwhere-clausecontains

提问by Gopinagh.R

Is there a function in postgres like contains? that can be used in the where clauseto 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:

有很多方法可以解决这个问题:

  1. Use like, ilike, and/or SIMILAR TOalong with ||. To handle columns, something like:

    WHERE col1 ilike '%' || col2 || '%';
    
  2. Use position as NPE's answer

  3. You could also use regexp_matchesbut that is more complex.

  1. 使用like, ilike, 和/或SIMILAR TO与 || 一起使用。要处理列,例如:

    WHERE col1 ilike '%' || col2 || '%';
    
  2. 使用位置作为 NPE 的答案

  3. 您也可以使用,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