malcoauri
malcoauri

Reputation: 12179

How to make a query with AND and OR operators?

I have the following query:

"SELECT COUNT(*) FROM Persons WHERE {1}='A' AND {2}!='A' AND {3}!='A'"

and I have the following task: to get all entries from table Person where {1} is A and {2} or {3} are not 'A', i.e. ABC, ABA, etc. Now I can get only ABC, ABB, etc, not ABA. Please, help me.

Upvotes: 0

Views: 108

Answers (2)

RichardTheKiwi
RichardTheKiwi

Reputation: 107696

Logic-wise simplification, compare {2} and {3} together?

SELECT COUNT(*)
FROM Persons
WHERE {1}='A' AND {2}+{3} != 'AA'

Without parenthesis, you can check this out Operator Precedence

SELECT COUNT(*)
FROM Persons
WHERE {1}='A' AND {2}!='A'
   OR {1}='A' AND {3}!='A'

Upvotes: 0

gtgaxiola
gtgaxiola

Reputation: 9331

Use Parenthesis to give Precedence to the logical operators

"SELECT COUNT(*) FROM Persons WHERE {1}='A' AND  ({2}!='A' OR {3}!='A')"

Upvotes: 2

Related Questions