Postgres-关于值是否包含特定字符串的WHERE子句

tsc*_*zle 6 sql string postgresql where

如果我想检索所有条目以使列foo值包含一个字符串'bar',是否有一种简单的方法可以在SQL或Postgresql中做到这一点?

有点像' WHERE foo = "bar"'但并非如此的=像' WHERE foo CONTAINS "bar"'。

Postgres 9.3版

Nei*_*gan 8

之一:

  1. WHERE foo LIKE '%bar%'

  2. WHERE foo ILIKE '%bar%' --case insensitive

  3. WHERE foo ~* 'bar' --regex

  4. WHERE foo ~* '\ybar\y' --matches bar but not foobar (\y is word boundary)

  5. WHERE foo::tsvector @@ 'bar'::tsquery --matches bar but not foobar

您可以建立索引foo以使LIKE,ILIKE和regexp更快地搜索:http : //www.postgresql.org/docs/9.3/static/pgtrgm.html#AEN154464 或在全文列上使用GIN或GIST索引


Joh*_*dge 7

使用SQL LIKE语句。这样,您可以将a %用作通配符。因此,您的WHERE声明可能类似于:

WHERE foo LIKE '%bar%'
Run Code Online (Sandbox Code Playgroud)