Rocco The Taco
Rocco The Taco

Reputation: 3777

MySQL query still returns value when NOT LIKE wildcard is used

I have a query where I'm using a wildcard value in conjunction with NOT LIKE as I'm looking for any variation of a company name. I've tried to move the NOT LIKE portion to the beginning of the WHERE statement and after the LIKE statement but both still return values with ABC Realty or ABC Realty, Inc. in the results? What am I missing?

SELECT * 
FROM mytable
WHERE ListOfficeName NOT LIKE 'ABC Realty%' AND 
      MLSAreaMajor LIKE 'NA01%' OR MLSAreaMajor LIKE 'NA02%' OR
      MLSAreaMajor LIKE 'NA03%' OR MLSAreaMajor LIKE 'NA04%' OR 
      MLSAreaMajor LIKE 'NA05%' AND PhotoCount >= '1' AND 
      Longitude BETWEEN -89.69 AND -72.07 
ORDER BY ListPrice DESC

Upvotes: 0

Views: 35

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72175

You have to use parentheses in order to enclose all OR predicates of the WHERE clause:

SELECT * 
FROM mytable
WHERE ListOfficeName NOT LIKE 'ABC Realty%' AND 
      (MLSAreaMajor LIKE 'NA01%' OR 
       MLSAreaMajor LIKE 'NA02%' OR 
       MLSAreaMajor LIKE 'NA03%' OR 
       MLSAreaMajor LIKE 'NA04%' OR 
       MLSAreaMajor LIKE 'NA05%') AND 
      PhotoCount >= '1' AND 
      Longitude BETWEEN -89.69 AND -72.07 
ORDER BY ListPrice DESC

Upvotes: 1

Related Questions