SQL Select values Where Like - multiple columns (name, surname)

How can I SELECT values using WHERE and LIKE, from multiple columns ? For example when I have user name in one column and surname in another column. I need to use WHERE LIKE '%name surname%'.
0
give a positive ratinggive a negative rating
Hi,

If you need to get the results from database, matching the beginning of user name, surname, name and surname, surname and name, middle name, you can use the solution below, based on SQL CONCAT(). You have to add some whitespace there to get the expected match.

SELECT id_user, user_name, user_surname FROM users
WHERE CONCAT(' ', user_name) LIKE '% " . $search_value . "%'
OR CONCAT(' ', user_surname) LIKE '% ". $search_value . "%'
OR CONCAT(user_name, ' ', user_surname) LIKE '" . $search_value . "%'
OR CONCAT(user_surname, ' ', user_name) LIKE '" . $search_value . "%';

Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2025 AnswerTabsTermsContact us