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
12 Dec 2022 at 06:33 PM
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 . "%';

0
give a positive ratinggive a negative rating
13 Dec 2022 at 09:34 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us