211

I have this MySQL query.

I have database fields with this contents

sports,shopping,pool,pc,games 
shopping,pool,pc,games 
sports,pub,swimming, pool, pc, games   

Why does this like query does not work? I need the fields with either sports or pub or both?

SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')

9 Answers 9

438

Faster way of doing this:

WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'

is this:

WHERE interests REGEXP 'sports|pub'

Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950

More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm

Sign up to request clarification or add additional context in comments.

13 Comments

if you are passing in an unknown amount of keywords as a string (a|b|c...), the regexp is the only way to go if you want to do LIKE, is it?
Do you know if this can be done with a subquery? Let's say I have a column of words I need to search for, how can I replace 'sports|pub' with a subquery?
Hey, could you please tell me the REGEXP for LIKE instead of %LIKE%, I am trying to fetch exact strings...
is it possible to use something like this format? WHERE interests REGEXP 'ex1|ex2|ex3|ex4|ex5|ex6|.... ex100' ??? what's its limitation?
To get the regexp value from a column: (select group_concat(myColumn separator '|') from..)
|
162

The (a,b,c) list only works with in. For like, you have to use or:

WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'

2 Comments

This wouldn't be beneficial in multiple (let's say 5 or more dynamic searchable query), thus, it'd be better to use regexp.
@ShayanAhmad What do you mean by beneficial? In terms of creating the query or query exection time? Isn't LIKE a lot more otpmized than REGEXP ?
44

Why not you try REGEXP. Try it like this:

SELECT * FROM table WHERE interests REGEXP 'sports|pub'

4 Comments

Yeah!! I want it the opposite way. So it is SELECT * FROM table WHERE interests NOT REGEXP 'sports|pub' (>‿◠)✌
How this answer is different from jazkat answer submitted 5 years before yours?
@Vaidas - thank you - was asking myself the same question ... :D
it's sort and simple (●'◡'●)
36

You can also use REGEXP's synonym RLIKE as well.

For example:

SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'

1 Comment

Just note for everyone that RLIKE and REGEXP are synonym
20

Or if you need to match only the beginning of words:

WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'

you can use the regexp caret matches:

WHERE interests REGEXP '^sports|^pub'

https://www.regular-expressions.info/anchors.html

2 Comments

this is not working, i have values like "1, 2,3, 30, 20" . So when I run query WHERE interests REGEXP '3^|2^' it will not return any data.
"if you need to match only the beginning of words" it should be: WHERE interests REGEXP '^(sports|pub)'
11

Don't forget to use parenthesis if you use this function after an AND parameter

Like this:

WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')

Comments

8

Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0

What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.

1 Comment

I think it should be SELECT * FROM table WHERE find_in_set(interests, 'sports,pub'), but this technique is likely to outperform regex in most situations.
3

More work examples:

SELECT COUNT(email) as count FROM table1 t1 
JOIN (
      SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
     ) t2 
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference';

Task was count participants at an event(s) with filter if email extension equal to multiple company domains.

Comments

2

Like @Alexis Dufrenoy proposed, the query could be:

SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0

More information in the manual.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.