Rails 中区分大小写的查找
我使用带有排序规则 utf8_general_ci
的 Mysql,对于我的大多数搜索来说,它都很好。但对于一个模型和一个字段,我想找到一条区分大小写的记录。怎么做呢?
I'm using Mysql with collation utf8_general_ci
and for most of my searches it is good. But for one model and one field I want to find a record with case sensitive. How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQL 正在执行不区分大小写的查询,而不是 Ruby on Rails。
请参阅http://dev.mysql.com/doc/refman /5.0/en/case-sensitivity.html
您可以将需要区分大小写的数据库列修改为区分大小写的
-
或者您可以修改查询以使用 COLLATE 运算符:
SELECT * from tbl_name WHERE col_name COLLATE latin1_bin LIKE 'a%'
It is MySQL that is doing the case insensitive query, not Ruby on Rails.
See http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html
You could make database columns, that require case sensitivity to be case sensitive
-
Or you can modify your queries to use COLLATE operator:
SELECT * from tbl_name WHERE col_name COLLATE latin1_bin LIKE 'a%'
如果您总是想以区分大小写的方式搜索该列,最好的办法是使用排序规则 utf8_bin 定义它
If you always want to search that column in a case sensitive manner, the best thing would be to define it with collation utf8_bin