JayIsTooCommon
JayIsTooCommon

Reputation: 1714

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 ?

Upvotes: 0

Views: 321

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

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."%'

Upvotes: 5

Related Questions