在我的rails应用程序中,我有一个SQL查询,用于搜索查询(params [:q])。如何更改它以便查询/:q不能在行内的任何位置,但必须与它一起开始?代码如下:
AccountNumber.where("account_number like?", "%#{params[:q]}%")
答案 0 :(得分:9)
从字符串中删除前导%
通配符。
AccountNumber.where("account_number like?", "#{params[:q]}%")
答案 1 :(得分:1)
只需删除前面的'%'
即可 像这样AccountNumber.where("account_number like?", "#{params[:q]}%")
答案 2 :(得分:1)
可以在查询中的任何位置使用通配符'%',以指示可以在那里找到任意长度的字符串。删除第一个'%'以搜索仅以#{params [:q]}开头的结果,而不是包含它:
AccountNumber.where("account_number like?", "#{params[:q]}%")