How DIFFERENCES between tables can be implemented with the help of MySQL joins?


We can get the differences between the tables by unioning exclusion joins from 1st table to 2nd table and from 2nd table to 1st table. To understand it, we are taking the example of following two tables −

mysql> Select * from value1;
+-----+-----+
| i   | j   |
+-----+-----+
|   1 |   1 |
|   2 |   2 |
+-----+-----+
2 rows in set (0.00 sec)

mysql> Select * from value2;
+------+------+
| i    | j    |
+------+------+
|    1 |   1  |
|    3 |   3  |
+------+------+
2 rows in set (0.00 sec)

Now, the following query will do the DIFFERENCE between tables ‘value1’ and ‘value2’ −

mysql> Select * from value1 left join value2 using(i,j) where value2.i is NULL UNION Select * from value2 left join value1 using(i,j) Where value1.i is NULL;
+------+-----+
| i    | j   |
+------+-----+
|    2 |   2 |
|    3 |   3 |
+-----+------+
2 rows in set (0.07 sec)

Updated on: 20-Jun-2020

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements