Why “SELECT … WHERE id=1=0” returns all rows except one with id=1?












8















Why the following query:



SELECT * FROM myTable WHERE id=1=0


returns all rows from myTable except one which has id=1?



myTable content:



+----+-------+
| id | value |
+----+-------+
| 1 | dog |
| 2 | cat |
| 3 | parrot|
+----+-------+


Now run: SELECT * FROM myTable WHERE id=1=0



Output:



+----+-------+
| id | value |
+----+-------+
| 2 | cat |
| 3 | parrot|
+----+-------+









share|improve this question


















  • 4





    Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

    – jarlh
    Jan 23 at 12:25








  • 2





    ... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

    – Thorsten Kettner
    Jan 23 at 12:37













  • @jarlh I have used your logic as answer. As you didn't posted as answer.

    – Muhammad Waheed
    Jan 23 at 13:21
















8















Why the following query:



SELECT * FROM myTable WHERE id=1=0


returns all rows from myTable except one which has id=1?



myTable content:



+----+-------+
| id | value |
+----+-------+
| 1 | dog |
| 2 | cat |
| 3 | parrot|
+----+-------+


Now run: SELECT * FROM myTable WHERE id=1=0



Output:



+----+-------+
| id | value |
+----+-------+
| 2 | cat |
| 3 | parrot|
+----+-------+









share|improve this question


















  • 4





    Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

    – jarlh
    Jan 23 at 12:25








  • 2





    ... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

    – Thorsten Kettner
    Jan 23 at 12:37













  • @jarlh I have used your logic as answer. As you didn't posted as answer.

    – Muhammad Waheed
    Jan 23 at 13:21














8












8








8


2






Why the following query:



SELECT * FROM myTable WHERE id=1=0


returns all rows from myTable except one which has id=1?



myTable content:



+----+-------+
| id | value |
+----+-------+
| 1 | dog |
| 2 | cat |
| 3 | parrot|
+----+-------+


Now run: SELECT * FROM myTable WHERE id=1=0



Output:



+----+-------+
| id | value |
+----+-------+
| 2 | cat |
| 3 | parrot|
+----+-------+









share|improve this question














Why the following query:



SELECT * FROM myTable WHERE id=1=0


returns all rows from myTable except one which has id=1?



myTable content:



+----+-------+
| id | value |
+----+-------+
| 1 | dog |
| 2 | cat |
| 3 | parrot|
+----+-------+


Now run: SELECT * FROM myTable WHERE id=1=0



Output:



+----+-------+
| id | value |
+----+-------+
| 2 | cat |
| 3 | parrot|
+----+-------+






mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 23 at 12:24









kozoohkozooh

97311027




97311027








  • 4





    Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

    – jarlh
    Jan 23 at 12:25








  • 2





    ... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

    – Thorsten Kettner
    Jan 23 at 12:37













  • @jarlh I have used your logic as answer. As you didn't posted as answer.

    – Muhammad Waheed
    Jan 23 at 13:21














  • 4





    Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

    – jarlh
    Jan 23 at 12:25








  • 2





    ... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

    – Thorsten Kettner
    Jan 23 at 12:37













  • @jarlh I have used your logic as answer. As you didn't posted as answer.

    – Muhammad Waheed
    Jan 23 at 13:21








4




4





Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

– jarlh
Jan 23 at 12:25







Evaluates as WHERE (id = 1) = 0... When ID = 1 it ends up as 1 which <> 0.

– jarlh
Jan 23 at 12:25






2




2





... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

– Thorsten Kettner
Jan 23 at 12:37







... and in MySQL 0 equals false. id = 1 is an expression that results in a boolean value (true, false or null), so MySQL expects a boolean on the right side of = . It finds 0, which it interprets as false.

– Thorsten Kettner
Jan 23 at 12:37















@jarlh I have used your logic as answer. As you didn't posted as answer.

– Muhammad Waheed
Jan 23 at 13:21





@jarlh I have used your logic as answer. As you didn't posted as answer.

– Muhammad Waheed
Jan 23 at 13:21












2 Answers
2






active

oldest

votes


















9














The reason is that the logic should be being evaluated as:



WHERE (id = 1) = 0


This is equivalent to:



WHERE (id = 1) "is false"


Or:



WHERE id <> 1


Try running these examples:



select 1=1=0, 1=2=3, 1=1=0





share|improve this answer































    7














    Default operator precedence works as follows :



    WHERE (ID=1)=0


    Which resutls false in case of id=1






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54327222%2fwhy-select-where-id-1-0-returns-all-rows-except-one-with-id-1%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      The reason is that the logic should be being evaluated as:



      WHERE (id = 1) = 0


      This is equivalent to:



      WHERE (id = 1) "is false"


      Or:



      WHERE id <> 1


      Try running these examples:



      select 1=1=0, 1=2=3, 1=1=0





      share|improve this answer




























        9














        The reason is that the logic should be being evaluated as:



        WHERE (id = 1) = 0


        This is equivalent to:



        WHERE (id = 1) "is false"


        Or:



        WHERE id <> 1


        Try running these examples:



        select 1=1=0, 1=2=3, 1=1=0





        share|improve this answer


























          9












          9








          9







          The reason is that the logic should be being evaluated as:



          WHERE (id = 1) = 0


          This is equivalent to:



          WHERE (id = 1) "is false"


          Or:



          WHERE id <> 1


          Try running these examples:



          select 1=1=0, 1=2=3, 1=1=0





          share|improve this answer













          The reason is that the logic should be being evaluated as:



          WHERE (id = 1) = 0


          This is equivalent to:



          WHERE (id = 1) "is false"


          Or:



          WHERE id <> 1


          Try running these examples:



          select 1=1=0, 1=2=3, 1=1=0






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 12:27









          Gordon LinoffGordon Linoff

          786k35310416




          786k35310416

























              7














              Default operator precedence works as follows :



              WHERE (ID=1)=0


              Which resutls false in case of id=1






              share|improve this answer




























                7














                Default operator precedence works as follows :



                WHERE (ID=1)=0


                Which resutls false in case of id=1






                share|improve this answer


























                  7












                  7








                  7







                  Default operator precedence works as follows :



                  WHERE (ID=1)=0


                  Which resutls false in case of id=1






                  share|improve this answer













                  Default operator precedence works as follows :



                  WHERE (ID=1)=0


                  Which resutls false in case of id=1







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 23 at 12:27









                  Muhammad WaheedMuhammad Waheed

                  638323




                  638323






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54327222%2fwhy-select-where-id-1-0-returns-all-rows-except-one-with-id-1%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules