JayIsTooCommon JayIsTooCommon - 1 month ago 9
MySQL Question

SQL PHP search LIKE two columns?

I have the below query;

SELECT * FROM `` WHERE `fname` LIKE 'argument' OR `lname` LIKE 'argument'


What do I need to change in order to 'concatenate' the columns so that I can search for rows
LIKE
fname.lname
?

Answer

Perhaps you want to concatenate the fields together for the like:

SELECT *
FROM staff 
WHERE concat_ws(' ', fname, lname) LIKE '%".$searchq."%';

If the comparison could go either way, then you might want two comparisons:

SELECT *
FROM staff 
WHERE concat_ws(' ', fname, lname) LIKE '%".$searchq."%' or
      concat_ws(' ', lname, fname) LIKE '%".$searchq."%'