什么情况下,下列语句会输出 t1_view 总行数?
select count(*) from t1_view where id not in (select id from t1_view);
以下为操作:
create table t1 (id int primary key);
create table t2 (id int primary key);
insert t1 values (1),(2),(3);
insert t2 values (1);
create view t1_view as select a.id from t1 a join t2 order by a.id;
MariaDB [tmp]> select count(*) from t1_view where id not in (select id from t1_view);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
MariaDB [tmp]> select count(*) from t1_view a where not exists (select id from t1_view b where a.id=b.id);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
当前用 not exists 结果合理,但在一些情况下也有问题.
MariaDB [tmp]> select count(*) from t1_view a where not exists (select id from t1 b where a.id=b.id);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
不知道是什么情况,现在很混乱. 希望懂的人解释一下原因.
版本 10.1.29-MariaDB-6+b1 Debian buildd-unstable