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

How to use MySQL joins to achieve differences between tables?

WBOY
Release: 2023-08-21 23:09:07
forward
1103 people have browsed it

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template