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

MySQLMySQLi Database

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)
raja
Published on 20-Feb-2018 16:34:31
Advertisements