<p>I am debugging some code and have encountered the following SQL query (simplified version):</p>

<pre><code>SELECT ads.*, location.county 
FROM ads
LEFT JOIN location ON location.county = ads.county_id
WHERE ads.published = 1 
AND ads.type = 13
AND ads.county_id = 2
OR ads.county_id = 5
OR ads.county_id = 7
OR ads.county_id = 9
</code></pre>

<p>I'm getting very strange results from the query and I think its because the first OR is negating the AND operators that are found before it.</p>

<p>This results in getting results back for ads of all types and not just for the type 13. </p>

<p>Each time the query is called there may be a differnt amount of county entities that need to be looked up.</p>

<p>Any help on the correct way to go about this would be appreciated.  </p>

Put parentheses around the "OR"s:
```
```
SELECT ads.*, location.county 
FROM ads
LEFT JOIN location ON location.county = ads.county_id
WHERE ads.published = 1 
AND ads.type = 13
AND
(
    ads.county_id = 2
    OR ads.county_id = 5
    OR ads.county_id = 7
    OR ads.county_id = 9
)
```
```
Or even better, use IN:
```
```
SELECT ads.*, location.county 
FROM ads
LEFT JOIN location ON location.county = ads.county_id
WHERE ads.published = 1 
AND ads.type = 13
AND ads.county_id IN (2, 5, 7, 9)
```
```