Reputation: 15
I'm trying to run a simple SQL query, and there's something that bothers me.
The syntax i'm using is:
select address_id, district, city_id from address
WHERE district = 'Alberta' OR district = 'QLD' OR district = 'Texas' OR district = 'Hamilton'
OR district = 'California' OR district = 'Washington'
AND address_id BETWEEN 3 and 300;
This is what i'm getting. Notice that i get numbers that are lower than 3 and higher than 300 for address_id. Why is that? Am i missing something?
Also, is there any way to group the "district" options together instead of separating them with "or district =" multiple times?
Thanks!
Upvotes: 0
Views: 34
Reputation: 105
All those or statements are messing up, try to filter like this:
WHERE district IN ('Alberta', 'QLD', 'Texas', 'Hamilton', 'California' , 'Washington') AND
address_id BETWEEN 3 and 300
Upvotes: 1
Reputation: 1269953
Because you need parentheses . . . or in
:
WHERE district IN ('Alberta', 'QLD', 'Texas', 'Hamilton', 'California' , 'Washington') AND
address_id BETWEEN 3 and 300
If you don't fully understand how AND
and OR
are processed, then you should use parentheses. You wrote:
WHERE district = 'Alberta' OR
district = 'QLD' OR
. . .
(district = 'Washington' AND address_id BETWEEN 3 and 300)
Upvotes: 1