Steven Dobbelaere
Steven Dobbelaere

Reputation: 211

Search full name in database with PHP when first and lastname are in different fields

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?

Upvotes: 1

Views: 4935

Answers (3)

manurajhada
manurajhada

Reputation: 5380

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

OR

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

Upvotes: 1

J A
J A

Reputation: 1766

Try using something like..

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

Upvotes: 5

Trickfire
Trickfire

Reputation: 443

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.

Upvotes: 0

Related Questions