在MySQL数据库中搜索全名或名字或姓氏,在单独的列中使用名字和姓氏

时间:2021-01-19 08:33:20

I'm working with an existing database that has first name and last name seperated in the database. I need to create a function that will take one search input and return results. say my database has a structure like....

我正在使用一个现有的数据库,它在数据库中分隔了名字和姓氏。我需要创建一个函数,它将获取一个搜索输入并返回结果。说我的数据库有像....

nameFirst nameLast
Joe       Smith
Joe       Jones
Joe       Brown

How could I, using MySql, take a search input that is say 'Joe Smith' and just get his row? But if I put just 'Joe' in the search field, return them all? Will I need to explode the string with a space? thanks!

我怎么能使用MySql获取一个搜索输入,就是说'Joe Smith'然后得到他的行?但是,如果我只在搜索字段中放入'Joe',那么将它们全部归还?我是否需要用空格来爆炸?谢谢!

1 个解决方案

#1


37  

SELECT * 
FROM table
WHERE CONCAT( nameFirst,  ' ', nameLast ) LIKE  '%Joe%'  

Be sure to sanitize any user submitted parameters such as "Joe"

务必清理任何用户提交的参数,例如“Joe”

#1


37  

SELECT * 
FROM table
WHERE CONCAT( nameFirst,  ' ', nameLast ) LIKE  '%Joe%'  

Be sure to sanitize any user submitted parameters such as "Joe"

务必清理任何用户提交的参数,例如“Joe”