53

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

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

Returns zero results

However

LIKE '%this%' and something=else

returns results and

LIKE '%that%' and something=else

returns result

Is it possible to get all my results into one query? If a string matches both, how will it handle that?

Oğuz Çelikdemir
  • 4,990
  • 4
  • 30
  • 56
Kevin Ohashi
  • 533
  • 1
  • 4
  • 5
  • If you had proper error checking you'd have gotten notified of the syntax error in the first query. At bare minimum you should have something like `mysql_query(...) or die(mysql_error());` – Marc B Jun 29 '11 at 21:09
  • 1
    Great answer as tutorial in related thread: http://stackoverflow.com/a/4808757/2906290 – Raúl Moreno Nov 06 '14 at 19:03

7 Answers7

97

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
34

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

ΔO 'delta zero'
  • 3,506
  • 1
  • 19
  • 31
Nijat Asad
  • 441
  • 4
  • 4
5

Try something like:

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

Dirk
  • 3,030
  • 1
  • 34
  • 40
3

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

(fieldname1 LIKE '%this%' or fieldname1 LIKE '%that%' ) and something=else
Ryan
  • 26,884
  • 9
  • 56
  • 83
2

Do you have something against splitting it up?

...FROM <blah> 
   WHERE 
     (fieldA LIKE '%THIS%' OR fieldA LIKE '%THAT%') 
     AND something = else
josh.trow
  • 4,861
  • 20
  • 31
1

Have you tried:

(column LIKE '%this%' and something=else) or (column LIKE '%that%' and something=else)
BumbleB2na
  • 10,723
  • 6
  • 28
  • 30
  • 1
    While this will work (and may lead to the exact same plan after optimizations), the non-distributed form is generally preferred -- at least for human consumption. –  Jun 29 '11 at 20:44
0

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.

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59