Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 非典型My SQL查询的执行顺序_Mysql_Sql_Execution - Fatal编程技术网

Mysql 非典型My SQL查询的执行顺序

Mysql 非典型My SQL查询的执行顺序,mysql,sql,execution,Mysql,Sql,Execution,假设我们有具有以下架构的表: create table Test(id integer, title varchar(100)); insert into Test(id, title) values(1, "Hello"); insert into Test(id, title) values(20, "Hi"); 在这种情况下,不典型的查询是: select * from Test where id= 20 < 2 AND SLEEP(1000

假设我们有具有以下架构的表:

create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Hello");
insert into Test(id, title) values(20, "Hi");
在这种情况下,不典型的查询是:

select * from Test where id= 20 < 2 AND SLEEP(1000000000) ;
-- sleep is being executed
select * from Test where id= 20 > 2 AND SLEEP(1000000000) ;
-- sleep is not being executed and nothing is going to be display
select * from Test where id = 20  > 0;
-- shows only one record , where id=20 /// "Hi"
select * from Test where id = 20  > -1;
-- shows whole table structure

在本例之前,我的问题是在特定的比较时刻发生了什么。为什么id=20>0时与id=20>-1时的行为不同。

下面是有问题的查询:

SELECT *
FROM Test
WHERE id = 20 > -1;
上面返回整个表的原因是WHERE子句实际上包含一个范围比较,其计算如下:

SELECT *
FROM Test
WHERE (id = 20) > -1;

表达式id=20将计算为1(如果为真),或0(如果为假)。在任何一种情况下,0和1都将始终大于-1,因此此查询将始终返回所有记录。

我知道是的,但id=20将被强制转换为1,并且相同的情况1>-1,1>0,那么为什么如果与0进行比较,它将只返回id=20的记录?例如,从id=20>0的测试中选择*;哦,它不是铸造1,因为1意味着错误,0是铸造的数字。。。这是真的吗。。?提前感谢,因为0>0是错误的。因此,您可以这样表述:id=20和id=20>0实际上是在断言相同的事情。