Rails + PostgreSQL -Using Like

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

I have the following two queries:

SELECT users.* FROM "users" WHERE (fname || lname LIKE '%james%')  SELECT users.* FROM "users" WHERE (fname || lname LIKE '%James%') 

I have a record in the User Table with fname = James

The problem I'm having is the first query returns 0 results, and the 2nd returns the correct result.

I want the LIKE to be case insensitive. Ideas? Thanks

回答1:

SELECT users.* FROM "users" WHERE (fname || lname ILIKE '%james%')

ILIKE = case-insenstive LIKE. Note that this is specific to PostgreSQL, and not a SQL standard.



回答2:

Try using

  SELECT users.* FROM "users" WHERE (fname || lname ILIKE '%james%') 

Notice the 'I' in LIKE



回答3:

Queries with "LIKE" or "ILIKE" are pretty slow, especially for tables with many entries. I think it would be faster if you use the full text search ability of PostgreSQL.

PostgreSQL textsearch docs



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!