SQL查询包含以PHRASE开头的整个单词的字段内容
我正在尝试编写一个 SQL 查询来获取字段包含多个关键字的行。该查询的用途是获取使用 jQuery 实现的自动完成器函数的结果。
现在,自动完成功能可以正常工作,但我的结果将包括特定字段中的内容包含查询的每一行。例如,如果我输入短语“ar”,我的结果将包括 car、guitar 、 bark 等。
我只想返回以该短语开头的结果,因为用户很可能不会输入以下的随机片段词的中间。我可以使用 Model.where((:name =~ "#{params[:q]}%")
第一个单词以查询开头的所有行> [我正在使用 meta_where
gem]。
但是,我还想返回字段中第二个单词以查询字符串开头的行,即使第一个单词不是这样
。 ,对于“blackdog”和“dogfood”行,查询字符串“do”应该返回这两行,而查询字符串“do”不应该返回“undone”。
我应该如何编写 SQL 查询。像这样搜索?
I am trying to write a SQL query to fetch rows whose fields contain multiple key words. The use for the query is to fetch results for an autocompleter function implemented with jQuery.
Right now, the autocompletion works, but my results will include every row whose content in a specific field contains the query. For example, if I type in the phrase 'ar', my results will include car, guitar , bark, etc.
I would like to return only results that begin with the phrase, as users will most likely not be typing in random fragments of the middle of words. I can fetch all rows where the first word begins with the query, by using Model.where((:name =~ "#{params[:q]}%")
[I am using the meta_where
gem].
However, I'd like to also return rows where the second word in the field begins with the query string, even though the first word does not.
For example, for the rows "black dog" and "dog food", the query string 'do' should return both of the rows, while the query string 'do' should not return 'undone'.
How should I write the SQL query for a search like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此结构仅适用于 meta_where 插件
This construction works with meta_where plugin only