当名字和名字在不同的字段中时,用PHP搜索数据库中的全名

当名字和名字在不同的字段中时,用PHP搜索数据库中的全名

问题描述:

I store the firstname and lastname in separate fields in the database. But I have to search the database for a full name. How can I do that in an efficient way using SQL and PHP?

我将firstname和lastname存储在数据库的不同字段中。 但我必须在数据库中搜索全名。 如何使用 SQL code>和 PHP code>以高效的方式实现这一目标? p> div>

Try using something like..

SELECT
       CONCAT_WS(' ', firstName,lastName) AS name
FROM 
       table
WHERE 
       name LIKE '%$keywords%'

SELECT * from names where CONCAT('first','last') = 'FirstLast';

Where the fields are first and last. Note that the CONCAT function in MySQL can do multiple arguements, so you could say:

CONCAT('first',' ','last')

To get 'John Doe' as the string you're comparing to; at this point, you can use = or like and compare just as if you were comparing against a normal string.

SELECT * FROM `test` WHERE CONCAT(first_name,' ',last_name)='Steven Dobbelaere';

OR

SELECT * FROM `test` WHERE CONCAT(first_name,' ',last_name) like '%keyword%';