cancel
Showing results for 
Search instead for 
Did you mean: 

SQL Anywhere 10 Select and Conditions

Former Member
2,846

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

Accepted Solutions (0)

Answers (2)

Answers (2)

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.

Former Member

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')