Prerequisites:
Assume there are two tables
Table1:
ID data
1
2 B
3 C
Table2:
ID data
1
2 B
4 d
Question:
1. If you want to find the same data in Table1 and Table2,
The SQL statement is: Select table1.id, table1.data, table2.id, table2.data
From Table1 inner join Table2 on (table1.data = table2.data) and (table1.id = table2.id );
The query result is:
| Table1_id |
Tabledomaindata |
Table2_id |
Table2_data |
| 1 |
A |
1 |
A |
| 2 |
B |
2 |
B |
2. If you want to query data in Table1 and also in Table2,
The SQL statement is: Select table1.id, table1.data, table2.id, table2.data
From Table1 left join Table2 on (table1.data = table2.data) and (table1.id = table2.id );
The query result is:
| Table1_id |
Tabledomaindata |
Table2_id |
Table2_data |
| 1 |
A |
1 |
A |
| 2 |
B |
2 |
B |
| 3 |
C |
|
|
3. If you want to find the data in Table2 and also in Table1,
The SQL statement is: Select table1.id, table1.data, table2.id, table2.data
From Table1 right join Table2 on (table1.data = table2.data) and (table1.id = table2.id );
The query result is:
| Table1_id |
Tabledomaindata |
Table2_id |
Table2_data |
| 1 |
A |
1 |
A |
| 2 |
B |
2 |
B |
|
|
4 |
D |
The SQL inner join, left join, right join code, and query results are as follows.
I hope to share more with you ....