Why “SELECT … WHERE id=1=0” returns all rows except one with id=1?
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
add a comment |
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
4
Evaluates asWHERE (id = 1) = 0
... When ID = 1 it ends up as 1 which <> 0.
– jarlh
Jan 23 at 12:25
2
... and in MySQL0
equalsfalse
.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 finds0
, which it interprets asfalse
.
– 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
add a comment |
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
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
mysql sql
asked Jan 23 at 12:24
kozoohkozooh
97311027
97311027
4
Evaluates asWHERE (id = 1) = 0
... When ID = 1 it ends up as 1 which <> 0.
– jarlh
Jan 23 at 12:25
2
... and in MySQL0
equalsfalse
.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 finds0
, which it interprets asfalse
.
– 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
add a comment |
4
Evaluates asWHERE (id = 1) = 0
... When ID = 1 it ends up as 1 which <> 0.
– jarlh
Jan 23 at 12:25
2
... and in MySQL0
equalsfalse
.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 finds0
, which it interprets asfalse
.
– 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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
Default operator precedence works as follows :
WHERE (ID=1)=0
Which resutls false in case of id=1
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Jan 23 at 12:27
Gordon LinoffGordon Linoff
786k35310416
786k35310416
add a comment |
add a comment |
Default operator precedence works as follows :
WHERE (ID=1)=0
Which resutls false in case of id=1
add a comment |
Default operator precedence works as follows :
WHERE (ID=1)=0
Which resutls false in case of id=1
add a comment |
Default operator precedence works as follows :
WHERE (ID=1)=0
Which resutls false in case of id=1
Default operator precedence works as follows :
WHERE (ID=1)=0
Which resutls false in case of id=1
answered Jan 23 at 12:27
Muhammad WaheedMuhammad Waheed
638323
638323
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
equalsfalse
.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 finds0
, which it interprets asfalse
.– 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