jamzsabb
jamzsabb

Reputation: 1154

SQLite WHERE clause being ignored

I've got a really simple query that is returning results that I'm pretty sure shouldn't be included.

CREATE TABLE hits
AS SELECT *
FROM deduped_table
WHERE Bank LIKE '%monex%' OR Bank LIKE '%cibanco%' OR Bank LIKE '%intercam%'
AND (Bank_Country = 'US' OR Bank_Country = 'United States');

When I look at the hits table, the Bank_Country column has results other than 'US' or 'United States'. Why?

Upvotes: 1

Views: 80

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114240

To complete @CL.'s answer, your query should read

SELECT *
FROM deduped_table
WHERE (Bank LIKE '%monex%'
   OR Bank LIKE '%cibanco%'
   OR Bank LIKE '%intercam%') AND
       (Bank_Country = 'US' OR Bank_Country = 'United States');

Upvotes: 3

CL.
CL.

Reputation: 180030

With parentheses added to show SQL's precedence rules, your query is:

SELECT *
FROM deduped_table
WHERE Bank LIKE '%monex%'
   OR Bank LIKE '%cibanco%'
   OR (Bank LIKE '%intercam%' AND
       (Bank_Country = 'US' OR Bank_Country = 'United States'));

Upvotes: 4

Related Questions