Home  >  Article  >  Database  >  How to use MySQL joins to achieve differences between tables?

How to use MySQL joins to achieve differences between tables?

WBOY
WBOYforward
2023-08-21 23:09:071039browse

How to use MySQL joins to achieve differences between tables?

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)

The above is the detailed content of How to use MySQL joins to achieve differences between tables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete