Search for a row in several fields of a table

I have a User table that has fields (id, first_name, middle_name, last_name).

I want to write a query to find a user by his name. The first name can be a first name, a first name or a last name.

$sql = "SELECT * FROM user 
        WHERE first_name like '%$name%' OR  
              middle_name like '%$name%' OR
              last_name like '%$name%'";

Is this an effective query? (Leave the security issue for now.)

+5
source share
3 answers

Modify the table and add compound full text in First_name,second_name,last_name, then use this query

select * 
from table_name 
where match (`First_name`,`second_name`,`last_name`) against('name')

This is much faster than your request.

+3
source

LIKE '%something%' WHERE, . , , LIKE .

- .

MySQL . .

+1

If you need to search for a template in several fields and if you have permission to change the table layout , I would suggest implementing FULL TEXT SEARCH .

Hope this helps :)

0
source

All Articles