简体   繁体   中英

Combining multiple 'AND' with 'LIKE' statements in PostgreSQL

Building a basic search tool in 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.

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'. Any help would be appreciated, thanks!

-Edit 1 Begins Here-

Sample Query that generates no data in 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. Using 'OR' in this scenario would retrieve records that won't match all the criteria I'm looking for.

I think you need OR instead AND

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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