177

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
  • 51,252
  • 26
  • 196
  • 186
webmasters
  • 5,383
  • 14
  • 47
  • 78

9 Answers9

384

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,428
  • 8
  • 23
jazkat
  • 5,212
  • 3
  • 24
  • 17
154

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
  • 225,110
  • 44
  • 364
  • 390
  • 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
43

Why not you try REGEXP. Try it like this:

SELECT * FROM table WHERE interests REGEXP 'sports|pub'
Ahmad Hussain
  • 2,397
  • 19
  • 26
32

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

For example:

SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Ruben Helsloot
  • 11,812
  • 5
  • 19
  • 40
iamharish15
  • 1,560
  • 14
  • 18
10

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
  • 18,308
  • 12
  • 99
  • 160
Luan
  • 101
  • 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,484
  • 11
  • 80
  • 123
  • 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
8

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
  • 903
  • 12
  • 28
  • 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
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
  • 20,627
  • 30
  • 92
  • 137
Franc Drobnič
  • 885
  • 10
  • 13
1

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
  • 474
  • 3
  • 6