Q:

SQL search multiple columns using LIKE %?%

I have a database of users with columns first_name', 'last_name' & 'email.
I am then trying to search using SQL like so:
SELECT * 
FROM customers 
WHERE email LIKE %?% OR 
      first_name LIKE %?% OR 
      last_name LIKE %?%";

So say my data in my database for those columns is this:
table tr td{
  padding:5px;
}

<table border="1">
<tr><td>first_name</td><td>last_name</td><td>email</td></tr>
<tr><td>test</td><td>user</td><td>user@test.com</td></tr>
</table>

In this case I can search for 'test' or I can search for 'user' and get the correct result but if I search for 'test user' there is no results.
What is the best way to go about searching across multiple columns like this where only part of the search string might be in one column and part in another column.

A:

You need a different query.  There is no way to express LIKE with multiple input parameters.
So, you could put all of your search terms in to a table, something along these lines...
SELECT
  *
FROM
  customers
INNER JOIN
(
            SELECT '%test%'   AS param
  UNION ALL SELECT '%user%'
)
  AS search
    ON customers.email      LIKE search.param
    OR customers.first_name LIKE search.param
    OR customers.last_name  LIKE search.param

(Or you could make an actual table instead of using an inline view.)

