How to find missing value between two MySQL Tables?

MySQLMySQLi Database

To find missing value between two MySQL tables, use NOT IN. Let us first create a table −

mysql> create table DemoTable1(Value int);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable1;

This will produce the following output −

+-------+
| Value |
+-------+
|     1 |
|     2 |
|     5 |
|     6 |
|     8 |
+-------+
5 rows in set (0.00 sec)

Following is the query to create second table −

mysql> create table DemoTable2(Value int);
Query OK, 0 rows affected (1.19 sec)

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable2;

This will produce the following output −

+-------+
| Value |
+-------+
|     1 |
|     2 |
|     3 |
|     4 |
+-------+
4 rows in set (0.00 sec)

Following is the query to find missing value between two MySQL Tables −

mysql> select Value from DemoTable1 where Value not in(select Value from DemoTable2);

This will produce the following output −

+-------+
| Value |
+-------+
|     5 |
|     6 |
|     8 |
+-------+
3 rows in set (0.07 sec)
raja
Published on 26-Aug-2019 08:12:45
Advertisements