'LIKE ('%this%' OR '%that%') and something=else' not working

前端 未结 7 1387
南方客
南方客 2020-12-15 14:53

I have a select query where I am trying to search strings for multiple patterns

LIKE (\'%this%\' or \'%that%\' ) and something=else

Returns

相关标签:
7条回答
  • 2020-12-15 15:36

    Instead of using LIKE, use REGEXP. For example:

    SELECT * WHERE value REGEXP 'THIS|THAT'
    
    mysql> SELECT 'pi' REGEXP 'pi|apa';                     -> 1
    mysql> SELECT 'axe' REGEXP 'pi|apa';                    -> 0
    mysql> SELECT 'apa' REGEXP 'pi|apa';                    -> 1
    mysql> SELECT 'apa' REGEXP '^(pi|apa)$';                -> 1
    mysql> SELECT 'pi' REGEXP '^(pi|apa)$';                 -> 1
    mysql> SELECT 'pix' REGEXP '^(pi|apa)$';                -> 0
    

    Refer: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

    0 讨论(0)
  • 2020-12-15 15:37

    It would be nice if you could, but you can't use that syntax in SQL.

    Try this:

    (column1 LIKE '%this%' OR column1 LIKE '%that%') AND something = else
    

    Note the use of brackets! You need them around the OR expression.
    Without brackets, it will be parsed as A OR (B AND C),which won't give you the results you expect.

    0 讨论(0)
  • 2020-12-15 15:41

    Have you tried:

    (column LIKE '%this%' and something=else) or (column LIKE '%that%' and something=else)
    
    0 讨论(0)
  • 2020-12-15 15:41

    I know it's a bit old question but still people try to find efficient solution so instead you should use FULLTEXT index (it's available from MySQL 5.6.4).

    Query on table with +35mil records by triple like in where block took ~2.5s but after adding index on these fields and using BOOLEAN MODE inside match ... against ... it took only 0.05s.

    0 讨论(0)
  • 2020-12-15 15:42

    Break out the LIKE clauses into 2 separate statements, i.e.:

    (fieldname1 LIKE '%this%' or fieldname1 LIKE '%that%' ) and something=else
    
    0 讨论(0)
  • 2020-12-15 15:48

    Try something like:

    WHERE (column LIKE '%this%' OR column LIKE '%that%') AND something = else

    0 讨论(0)
提交回复
热议问题