简体   繁体   English

在PostgreSQL中结合多个'AND'与'LIKE'语句

[英]Combining multiple 'AND' with 'LIKE' statements in PostgreSQL

Building a basic search tool in PostgreSQL. 在PostgreSQL中构建基本的搜索工具。

When I setup a basic query like below I receive data. 当我设置如下所示的基本查询时,我会收到数据。

SELECT *
FROM schema_name.table_name
WHERE column_name1 = 'sometext' AND column_name2 LIKE '%sometext%'
;

But when I and more conditions to the where clause like below, the query does execute, but I receive no data. 但是,当我对如下所示的where子句有更多条件时,查询会执行,但是我没有收到任何数据。

SELECT *
FROM schema_name.table_name
WHERE column_name1 = 'sometext' AND column_name2 LIKE '%sometext%' AND column_name3 = 'somemoretext'
;

I've tested individual queries to ensure there are records that meet all of the conditions and they exist. 我已经测试了单个查询,以确保有满足所有条件的记录并且它们存在。 However I cannot find a way to tie the criteria together into one query. 但是,我找不到一种将条件绑定到一个查询中的方法。 Most examples I've seen online stop at two conditions or do not combine 'AND' with 'LIKE'. 我在网上看到的大多数示例都在两种情况下停止,或者不将“ AND”与“ LIKE”结合使用。 Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

-Edit 1 Begins Here- 编辑1从这里开始

Sample Query that generates no data in PostgreSQL 在PostgreSQL中不生成数据的示例查询

SELECT *
FROM ctgov.studies
WHERE (overall_status = 'Recruiting') AND (official_title LIKE '%immunotherapy%') AND (source LIKE '%university%')
;

When I execute the same search via the website I gather data for the DB it returns 163 matching records. 当我通过网站执行相同的搜索时,我收集了数据库的数据,它返回了163条匹配记录。 Using 'OR' in this scenario would retrieve records that won't match all the criteria I'm looking for. 在这种情况下使用“ OR”将检索与我要查找的所有条件都不匹配的记录。

I think you need OR instead AND 我认为您需要OR AND

  SELECT *
    FROM schema_name.table_name
    WHERE column_name1 = 'sometext' OR column_name2 LIKE '%sometext%' OR column_name3 = 'somemoretext'

感谢大家的帮助,得知我的查询过于严格,我没有考虑到文本识别中的大写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM