SQL - WHERE using 2 conditions and AND as conditional - Answer is not the expected result

Asked

Viewed 49 times

1

Good afternoon, doubt is very basic.

I am counting items from a single table to two specific "classes" (M and N) within the same column (two possible outputs is what I am calling a "class"). When I use the WHERE command with only one class the displayed result is correct:

Count how many type M occurrences

SELECT COUNT(Name)
 
FROM Data$

WHERE Policy_Group_Name = 'M'

or

Count how many type N occurrences

SELECT COUNT(Name)
 
FROM Data$

WHERE Policy_Group_Name = 'N' 

But when I use the AND conditional and the two conditions I get 'O' or 'NULL' as a result:

Count how many occurrences of type M & N

SELECT COUNT(Name)
 
FROM Data$

WHERE Policy_Group_Name = 'M' AND Policy_Group_Name = 'N'

I don’t know where I’m going wrong, I know it must be something simple, but I can’t see, if anyone can help I appreciate.

Hug. Bia.

2 answers

2

You are using the condition AND. That is, you are looking for a result where the Policy_Group_Name equals "M" and equals "N".


The correct instruction in this case would be to use the `OR`condition, thus:
WHERE Policy_Group_Name = 'M' OR Policy_Group_Name = 'N'

Since so, you will be looking for results where Policy_Group_Name whether it’s "M" or "N". I hope I helped!

  • 1

    It did, thank you.

  • 1

    also WHERE Policy_group_name IN (’M' 'N')

2


The function of logical operators is to relate logical propositions. The main logical operators are:

AND
Será verdade se e somente se as duas expressões relacionais forem verdadeiras

OR
Será verdade quando ao menos uma das expressões relacionadas for verdade

NOT
Será verdade somente quando a expressão associada a ele for falsa

When relating the expressions Policy_Group_Name = 'M' and Policy_Group_Name = 'N' with the AND operator, your query fetches a field from the column where the value of Policy_Group_Name is’M' and 'N' simultaneously. I suggest you use the following expression:

Policy_Group_Name = 'M' OR Policy_Group_Name = 'N'

This way you will have return in this query of those fields where Policy_group_name is equal to’M' or 'N'.

  • I dropped this token eating lunch, but you were faster...rs. Sorry, I got the add in the head, then I went to code.... Then it went bad.... Thanks @dududornelees, @Gabriel Gomes Nicolim and @Ricardo Pontual.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.