197

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%')
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
webmasters
  • 5,663
  • 14
  • 51
  • 78

9 Answers9

418

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

Zer0
  • 1,580
  • 10
  • 28
jazkat
  • 5,600
  • 3
  • 25
  • 17
159

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

WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 5
    This wouldn't be beneficial in multiple (let's say 5 or more dynamic searchable query), thus, it'd be better to use regexp. – Shayan Ahmad Dec 14 '18 at 15:03
  • 1
    @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 ? – XCS Aug 23 '20 at 21:23
44

Why not you try REGEXP. Try it like this:

SELECT * FROM table WHERE interests REGEXP 'sports|pub'
Ahmad Hussain
  • 2,443
  • 20
  • 27
34

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

For example:

SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
iamharish15
  • 1,760
  • 1
  • 17
  • 20
15

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

vadim
  • 993
  • 13
  • 29
  • 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. – pratik vekariya Oct 23 '21 at 07:22
  • "if you need to match only the beginning of words" it should be: WHERE interests REGEXP '^(sports|pub)' – John Jul 23 '23 at 05:16
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%')
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
Luan
  • 111
  • 1
  • 2
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.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • 2
    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. – Chris Strickland Sep 14 '17 at 04:02
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.

4b0
  • 21,981
  • 30
  • 95
  • 142
Franc Drobnič
  • 985
  • 10
  • 14
2

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.

Intacto
  • 527
  • 3
  • 8