我使用 2 列(姓名和姓氏)创建了一个搜索应用程序,但当一个人的姓名超过 2 个时无法正常工作。如何搜索具有 3 个姓名和姓氏的人?
控制器:
class PersonController < ApplicationController
def search
@people = Person.find(:all,:conditions=>["(
name LIKE ? OR
lastname LIKE ? OR
(concat(name, \" \", lastname) LIKE ? )
)" ,
"%#{params[:query]}%",
"%#{params[:query]}%",
"%#{params[:query]}%" ]
)
end
end
看法
<% form_tag :controller=>"person",:action=>"search" do %>
Nombre o Apellido
<%= text_field_tag "query", params[:query]
<% end %>
例子
|name| |Lastname|
Marcos Ignacio Raul Perez
我在我的日志上看到了这个:如果我搜索 Marcos Ignacio Raul Perez 就可以了
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Ignacio Raul Perez%' OR
lastname LIKE '%Marcos Ignacio Raul Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Ignacio Raul Perez%' )
))
如果我搜索 Ignacio Raul Perez 就可以了
SELECT * FROM `people`
WHERE ((
name LIKE '%Ignacio Raul Perez%' OR
lastname LIKE '%Ignacio Raul Perez%'
OR (concat(name, " ", lastname) LIKE 'Ignacio Raul Perez%' )
))
如果我搜索 Raul Perez 就可以了
SELECT * FROM `people`
WHERE ((
name LIKE '%Raul Perez%' OR
lastname LIKE '%Raul Perez%'
OR (concat(name, " ", lastname) LIKE 'Perez%' )
))
如果我搜索 Marcos Perez 则不起作用
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Perez%' OR
lastname LIKE '%Marcos Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Perez%' )
))
如果我搜索 Ignacio Perez 则不起作用
SELECT * FROM `people`
WHERE ((
name LIKE '%Ignacio Perez%' OR
lastname LIKE '%Marcos Perez%'
OR (concat(name, " ", lastname) LIKE '%Ignacio Perez%' )
))
如果我搜索 Marcos Raul Perez 则不起作用
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Raul Perez%' OR
lastname LIKE '%Marcos Raul Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Raul Perez%' )
))
我将非常感谢帮助。