版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011136197/article/details/80992185
执行sql:
select 1 from dual where exists (select 0);
Query OK, 0 rows affected (0.44 sec)
mysql> insert into t3 values(1,'20160812');
Empty set (0.15 sec)
mysql> select 1 from dual where exists (select max(id) from t3 where id=2);
+---+
| 1 |
+---+
+---+
| 1 |
+---+
| 1 |
mysql> select id from t3 where id=2;
+---+
| NULL |
+---+
+---+
| 0 |
+---+
| 0 |
select 1 from dual where exists (select 0);
select 1 from dual where exists (select null);
执行结果都是1;
Query OK, 0 rows affected (0.44 sec)
mysql> insert into t3 values(1,'20160812');
Query OK, 1 row affected (0.16 sec)
mysql> select 1 from dual where exists (select id from t3 where id=2);Empty set (0.15 sec)
mysql> select 1 from dual where exists (select max(id) from t3 where id=2);
+---+
| 1 |
+---+
| 1 |
mysql> select 1 from dual where exists (select count(id) from t3 where id=2);+---+
| 1 |
+---+
| 1 |
mysql> select id from t3 where id=2;
Empty set (0.15 sec)
mysql> select max(id) from t3 where id=2+---+
| NULL |
+---+
| NULL|
mysql> select count(id) from t3 where id=2+---+
| 0 |
+---+
| 0 |
由此可见:exists里子查询执行结果是null或者0,只要有一行结果,则exist就生效执行出结果,只有子查询执行结果是Empty的时候exists才会过滤数据。