开发者

SQL Where clause 'that begins with'?

In my rails application i have an SQL query tha tuses where 开发者_StackOverflowand like to search for a query (params[:q]). How can i change it so query/:q cant be anywhere within the row, but must BEGIN with it? Here the code:

 AccountNumber.where("account_number like?", "%#{params[:q]}%")


Remove the leading % wildcard from the string.

AccountNumber.where("account_number like?", "#{params[:q]}%")


Just remove the '%' at the front

like this

AccountNumber.where("account_number like?", "#{params[:q]}%")

http://en.wikipedia.org/wiki/Percent_sign


The wildcard character '%' can be used anywhere in the query to indicated that a string of any length can be found there. Remove the first '%' to search for results that only start with the #{params[:q]} rather than contains it:

AccountNumber.where("account_number like?", "#{params[:q]}%")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜