There are a few ways that I can think of you can do this:
- You could run the
LIKE
against both the first_name
and last_name
columns separately using an OR
, or
- You could use a sub-query to concatenate the fields first, or
- You could run the
LIKE
against the concatenated fields.
For the first solution:
SELECT id,
first_name,
last_name,
concat(first_name,' ',last_name) AS full_name
FROM mytable
WHERE (first_name LIKE '%scott%' OR
last_name LIKE '%scott%');
For the second solution:
SELECT *
FROM (
SELECT id,
first_name,
last_name,
concat(first_name,' ',last_name) AS full_name
FROM mytable
) a
WHERE full_name LIKE '%scott%';
The third involves including WHERE CONCAT(...) LIKE '%scott%'
.
I would personally use the second solution as I believe it would be more efficient, and I think it looks more presentable. They'll all work though so it's up to you.