on 2013 Aug 21 11:46 PM
I am having trouble querying a SQL Anywhere Database to select one column only if all conditions are met. Here is an example:
SELECT USER FROM TABLE1 WHERE DATA0 < 1 AND DATA1 = 'F' AND ID = 23 AND ID = 232 AND CITY = 'CITY1' OR CITY = 'CITY2' OR CITY = 'CITY3'
So I want USER to be returned only if all the conditions are true
Request clarification before answering.
Because AND
is above OR
in SQL Anywhere's operator precedence you need to place your last three conditions in parenthesis:
SELECT USER FROM TABLE1 WHERE DATA0 < 1 AND DATA1 = 'F' AND ID = 23 AND ID = 232 AND (CITY = 'CITY1' OR CITY = 'CITY2' OR CITY = 'CITY3')
Otherwise you will get every row that satisfies one of the OR'ed CITY
conditions.
EDIT: As crb pointed out, this query will not likely return the desired result because you are requiring ID to equal both 23 and 232. These should also be in a OR/IN statement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And if you want ID to be OR'ed as well, using the IN operator, you can use WHERE ID IN (23, 232) AND CITY IN ('CITY1', 'CITY2', 'CITY3')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
52 | |
8 | |
5 | |
5 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.