2

Here is what I was want to Optimize this query

SELECT * 
FROM users 
where `username` like "%abc%" 
   or `name`     like "%abc%" 
   or `email`    like "%abc%"

This search get result from users where any of username , name , email like %abc%

So I tried to use in with like But

I was looking for mysql Like in and I have found this answer stackoverflow :Mysql like in

So the solution was regex

But I want to make the regex not use OR

I want to be like this

SELECT * from users where abc REGEXP 'username|name|email';

So the answer

Community
  • 1
  • 1
Mona Abdelmajeed
  • 686
  • 1
  • 9
  • 19

1 Answers1

1

Do you mean something like this? >>

SELECT * from users where CONCAT(username, ',', name, ',', email) REGEXP 'abc'
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • Your query is very good for me , and the same result work with LIKE statment Is there any diffrent between regex and like with your query – Mona Abdelmajeed Sep 23 '12 at 13:33
  • @MonaAbdelmajeed - Your question asks for regex solution, so I used `REGEXP` in the example. In this case, you will get same results if you use `like` or `REGEXP`. However for more complex search/match patterns `REGEXP` is better choice... – Ωmega Sep 23 '12 at 13:36