如何搜索名字和姓氏?

时间:2014-08-12 23:55:59

标签: sql ruby-on-rails activerecord

我有这个搜索功能:

  def self.search(query)
    where('customer_phone_number LIKE ? OR lower(first_name) LIKE ? OR lower(last_name) LIKE ?', "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%")
  end

这很棒,但如果有人在搜索栏中输入第一个和最后一个名字,则不会返回任何内容。如果用户同时输入名字和姓氏,如何检查全名呢?

1 个答案:

答案 0 :(得分:1)

使用sql函数concat()

def self.search(query)
    where("customer_phone_number LIKE ? OR lower(first_name) LIKE ? OR lower(last_name) LIKE ? OR concat(lower(first_name),' ', lower(last_name)) like ?", "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%", "%#{query.downcase}%")
end
相关问题