Searching full name or first or last name in MySQL database with first and last name in separate columns

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

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


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

I suggest to use this if you are sure that your search input start with "Joe" to filter more.

In my humble opinion in a lot of cases we don't know if the search input is the name or the surname so I suggest to use this:

SELECT * FROM table WHERE CONCAT(nameFirst,  ' ', nameLast) LIKE  'SEARCH_INPUT%' OR CONCAT(nameLast,  ' ', nameFirst) LIKE 'SEARCH_INPUT%';

Tags:

Mysql

Php