OR returns unexpected result

Asked

Viewed 72 times

1

I’m performing that function

SELECT COUNT(id) FROM `system_stats` WHERE  `name` = 1 OR 0 AND `us_id` = 0

It returns me a value equal to 1, but this 1 no, when I run separately this way, it returns me 0, why with the OR he returns me 1?

SELECT COUNT(id) FROM `system_stats` WHERE  `name` = 1 AND `us_id` = 0
SELECT COUNT(id) FROM `system_stats` WHERE  `name` = 0 AND `us_id` = 0
  • name = 1 OR name = 0

  • Problem persists, name is INT with values of 0 or 1 @Papacharlie

1 answer

4


It returns like this because you’re in charge. The OR cannot be used loosely so it is not natural language text.

SELECT COUNT(id) FROM `system_stats` WHERE  `name` = 1 OR 0 AND `us_id` = 0

name = 1 according to you, gives false.

0 gives true, since that is a statement.

us_id = 0must be giving true

0 AND us_id = 0 gives true. This sub-expression is executed first by the rule of precedence. It is as if it were a multiplication in arithmetic.

False or true, gives true, then returns something you don’t want.

It’s called boolean algebra. If you don’t understand this you won’t be able to program.

Probably what you wanted to do is this:

SELECT COUNT(id) FROM `system_stats` WHERE (`name` = 1 OR `name` = 0) AND `us_id` = 0

I put in the Github for future reference.

Understand the precedence of operators is very important. You have to understand what you do before. Just like knowing that the product takes precedence over the sum, you have to know that the AND takes precedence over the OR and if you want to associate the two terms of OR before taking one of them and associating with the other term of the AND have to make this explicit in the code through the parentheses.

Other than that, if the problem is another your logic is more confused yet. Your question does not make it very clear what you really want. You need to learn to express yourself well in the program and out of it to achieve the expected results.

  • I understand, but even so it returns me the value 1, I’m lost.

  • What I want is to count how many Rows there are with NAME = 1 or 0 and with US_ID = 0, simple guess

  • 1

    But if the value of name is always 0 or 1 (as your comment above), why you need this condition in the search, @user3163662?

  • @bfavaretto always 0 or 1 in the already inserted Rows, but in the future I will put 0-19

  • 1

    It’s what I told you, you have to study programming consistently. It is not trying to do things in trial and error that you will understand all the necessary aspects. I could try to help more if I understood what you really want, what data you generate, what situation, etc. If you who have everything at your disposal can not solve, we who have not been in a more unfavorable situation to be able to help.

  • I think the problem was simply the precedence of the operators, their parentheses should solve.

  • The parentheses solved, thank you very much.

Show 2 more comments

Browser other questions tagged

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