Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 415
Kevin Ohashi Avatar asked Jun 29 '11 20:06

Kevin Ohashi


2 Answers

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.

like image 161
Bohemian Avatar answered Oct 17 '22 01:10

Bohemian


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

like image 31
Nijat Asad Avatar answered Oct 17 '22 03:10

Nijat Asad