如何使 LIKE 子句不区分大小写? [英] How to make LIKE clause case-insensitive?
本文介绍了如何使 LIKE 子句不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Ruby On Rails 中使用了 LIKE 子句.当我尝试通过键入 "more" 来搜索记录时,它不会返回任何内容,但是当我使用 "More" 进行搜索时,它会返回包含 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
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屋!
查看全文