如何使LIKE子句不区分大小写? [英] How to make LIKE clause case-insensitive?
本文介绍了如何使LIKE子句不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Ruby On Rails中使用LIKE子句。当我尝试通过键入更多 搜索记录时,它不会返回任何内容,但是当我使用更多 进行搜索时,它将返回包含 More 关键字,因此它的行为似乎区分大小写。
I'm using a LIKE clause in Ruby On Rails. When I try to search for records by typing "more" it doesn't return anything, but when I do with "More", then it returns the records which contains More keyword, so it seems like it behaves in a case-sensitive way.
是否可以使该大小写不敏感?
Is it possible to make this case-insensitive?
这是我当前正在使用的查询:
Here is the query I am using currently:
Job.where('title LIKE ? OR duration LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
推荐答案
我假设您正在使用Postgres。
I assume you're using Postgres.
您可以使用 ILIKE
You can use ILIKE
Job.where('title ILIKE ? OR duration ILIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
或者一些棘手的 lower()
:
Job.where('lower(title) LIKE lower(?) OR lower(duration) LIKE lower(?)', "%#{params[:search]}%", "%#{params[:search]}%")
这篇关于如何使LIKE子句不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文