2

I am making a search that searches a SQL table by multiple rows. For example:

| FirstName | LastName |
|----------------------|
| John      | Doe      |
|----------------------|

SELECT FirstName,LastName FROM Users WHERE FirstName LIKE '%inputhere%' OR LastName LIKE '%inputhere%' LIMIT 10

When I search a full name (e.g. John Doe) no results appear because no one row has that value.

I there a way to separate the search query by spaces in order to avoid this?

1
  • Investigate full text indexes. That is probably the best solution for what you want to do. Nov 1, 2015 at 18:33

2 Answers 2

2

Similar to what Gordon said, the best solution is to use Full Text indexes. However, You can do it like this:

Select * from Users where concat(FirstName, " ", LastName) like "John Doe"

You can feel free to turn "John Doe" to "%John Doe%" or "John Doe%".

Try it here: http://sqlfiddle.com/#!9/d0042/5/0

1

The best solution for what you want is full text indexes. However, you might be able to accomplish what you want with something like this:

where concat_ws(' ', firstname, lastname) like concat('%', replace($inputhere, ' ', '%'), '%') or
      concat_ws(' ', lastname, firstname) like concat('%', replace($inputhere, ' ', '%'), '%')

This is brute force and requires a full table scan for each query (which is okay for small tables).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.