[英]Condition query with LIKE does not match an exact match
I'm using the following query to search for users: 我正在使用以下查询来搜索用户:
@users = User.find( :all,
:select => 'users.*',
:conditions => ["lower(fname) || ' ' || lower(lname) LIKE ?", '%'+"#{params[:q].downcase}"+'%'],
)
This works great for examples like so: 这适用于以下示例:
Bob S find Bob Smith
Bob finds Bob Smith
Problem is 问题是
Bob Smith does not find Bob Smith
How can I get the query above to return an exact match? 如何才能获得上面的查询以返回完全匹配? Thanks 谢谢
The query is just plain wrong. 查询完全错误。 First, '||' 首先,'||' is not a concatenation operator. 不是连接运算符。 Second, the operator precedence makes it evaluated as: 其次,运算符优先级使其评估为:
(lower(fname)) || (' ') || (lower(lname) LIKE '%Bob Smith%')
which is not what you want. 这不是你想要的。 I guess you would like it: 我想你会喜欢它:
concat(lower(fname), ' ', lower(lname)) LIKE '%Bob Smith%'
EDIT: So you can use: 编辑:所以你可以使用:
@users = User.find( :all,
:select => 'users.*',
:conditions => ["concat(lower(fname), ' ', lower(lname)) LIKE ?", "%#{params[:q].downcase}%"])
EDIT2: The above is wrong, I haven't noticed it was about Postgresql. EDIT2:以上是错误的,我没有注意到它是关于Postgresql的。 There is a '||' 有一个'||' concatenation operator in Postgres of course. Postgres中的串联运算符当然。 I'm not deleting this post because of the comments below but don't use it please. 我不是因为下面的评论而删除这篇文章,但请不要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.