首页 > 文章列表 > 如何利用MySQL的连接来实现表之间的差异?

如何利用MySQL的连接来实现表之间的差异?

135 2023-09-05

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)