如何找出两张 MySQL 表格之间的缺失值?
若要查找两张 MySQL 表格之间的缺失值,请使用 NOT IN。让我们首先创建一个表格 -
mysql> create table DemoTable1(Value int); Query OK, 0 rows affected (0.56 sec)
使用插入命令在表格中插入一些记录 -
mysql> insert into DemoTable1 values(1); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(2); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable1 values(5); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1 values(6); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1 values(8); Query OK, 1 row affected (0.16 sec)
使用选择语句在表格中显示所有记录 -
mysql> select *from DemoTable1;
这将生成以下输出 -
+-------+ | Value | +-------+ | 1 | | 2 | | 5 | | 6 | | 8 | +-------+ 5 rows in set (0.00 sec)
以下是创建第二张表格的查询 -
mysql> create table DemoTable2(Value int); Query OK, 0 rows affected (1.19 sec)
使用插入命令在表格中插入一些记录 -
mysql> insert into DemoTable2 values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2 values(2); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2 values(3); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2 values(4); Query OK, 1 row affected (0.12 sec)
使用选择语句在表格中显示所有记录 -
mysql> select *from DemoTable2;
这将生成以下输出 -
+-------+ | Value | +-------+ | 1 | | 2 | | 3 | | 4 | +-------+ 4 rows in set (0.00 sec)
以下是查找两张 MySQL 表格之间缺失值的查询 -
mysql> select Value from DemoTable1 where Value not in(select Value from DemoTable2);
这将生成以下输出 -
+-------+ | Value | +-------+ | 5 | | 6 | | 8 | +-------+ 3 rows in set (0.07 sec)
广告