我怎么可以用这样的查询红宝石与西纳特拉? [英] how can I use like query in ruby with sinatra?
问题描述
下面是我想查询:
@blogs = DB[:blogs].where(:title => params[:s_txt]).reverse_order(:id)
在此查询,我想找到我的数据库中的博客。我还需要创建一个查询,为用户提供更多的结果。我怎样才能做到这一点?
In this query, I'd like to find blogs in my database. I also need to create a query that gives users more results. How can I do this?
推荐答案
我也有这个问题,才发现这个解决方案
I also had this problem and only found this solution
@blogs = DB[:blogs].where("title LIKE '%#{params[:s_txt]}%'").reverse_order(:id)
好奇,如果有更好的方法。
Curious if there are better ways..
在搜索中的两个领域是重复等,通过分隔条件AND或OR
Searching in two fields is repeating the LIKE and seperating by AND or OR
@blogs = DB[:blogs].where("title LIKE '%#{params[:s_txt]}%'
or comment LIKE '%#{params[:c_txt]}%'").reverse_order(:id)
或者像这样
@blogs = DB[:blogs].where("title||comment LIKE '%#{params[:s_txt]}%'").reverse_order(:id)
测试SQLite中和它的作品。一个提醒:在拉赫表你最好如果可能下降的第一个%,因为该数据库将做一个全表扫描,否则不使用索引,但你可以找到所有的东西在sql问题
Tested in Sqlite and it works. One advise: in lage tables you 'd better drop the first % if possible because the database will do a full table scan otherwise without using indexes but you can find all that stuff in the sql questions.
这篇关于我怎么可以用这样的查询红宝石与西纳特拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!