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 |...

问题:

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|
+----+-------+

回答1:

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

回答2:

Default operator precedence works as follows :

WHERE (ID=1)=0

Which resutls false in case of id=1

  • 发表于 2019-01-28 22:10
  • 阅读 ( 63 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除