简体   繁体   English

如何搜索完全匹配

[英]How to search for exact match

I am using Rails 4, a MySQL database, and jQuery Datatables 1.10. 我正在使用Rails 4,MySQL数据库和jQuery Datatables 1.10。 I have a search function that works, but returns more results than I'd like. 我有一个有效的搜索功能,但返回的结果超出了我的期望。

For example, when I search for "Phase I" it returns results with the fields "Phase I", "Phase II", or "Phase III" because "Phase II", and "Phase III" both contain "Phase I". 例如,当我搜索“阶段I”时,它返回带有“阶段I”,“阶段II”或“阶段III”字段的结果,因为“阶段II”和“阶段III”都包含“阶段I”。

This is the Query: 这是查询:

table = table.where("col_name like :search", search: "%#{query}%")

How can I change this search to only return "Phase I" and not "Phase II" or "Phase III"? 如何更改此搜索以仅返回“第一阶段”而不返回“第二阶段”或“第三阶段”?

Your query would be 您的查询将是

Table.where(col_name: query)

That should give you an exact match 那应该给你一个精确的匹配

此解决方案为我工作:

table = table.where("col_name = '#{query}'")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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