SQL left join, right join, and inner join
Today, I chatted with a friend about a small problem in their company. As follows:
Table A device table stores MAC addresses, provinces, cities, and zones.
Table B Software Table, which stores MAC addresses and software names.
You can query the Software List by province, city, or district.
Guess how it works now?
It obtains the MAC address through the province, city, or district, and then queries table B using in.
This is obviously unreasonable. Why don't we need to perform multi-table join queries to deal with this multi-to-many relationship?
There are three commonly used linked list methods: (inner) join Internal equivalent join, left join and right join.
What is the difference? How to use it? The following is an article about copy:
Left join returns records that include all records in the left table and join fields in the right table.
Right join returns records that include all records in the right table and the joined fields in the left table.
Inner join (equivalent join) returns only rows with equal join fields in two tables.
Example:
Table A records the following:
Copy codeThe Code is as follows:
AID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
Table B records the following:
Copy codeThe Code is as follows:
BID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
1. left join
The SQL statement is as follows:
Copy codeThe Code is as follows:
Select * from A left join B on A. aID = B. bID
The result is as follows:
Copy codeThe Code is as follows:
AID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL
(The number of affected rows is 5)
Result description:
Left join is based on the records of table A. A can be seen as the left table, B can be seen as the right table, and left join is based on the left table.
In other words, the records in the left table (A) are all expressed, while the right table (B) only displays records that meet the search criteria (in this example:. aID = B. bID ).
All records in Table B are NULL.
2. right join
The SQL statement is as follows:
Copy codeThe Code is as follows:
Select * from A right join B on A. aID = B. bID
The result is as follows:
Copy codeThe Code is as follows:
AID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL 8 2006032408
(The number of affected rows is 5)
Result description:
After careful observation, we will find that the result of left join is exactly the opposite. This time, it is based on the right table (B) and is filled with NULL when table A is insufficient.
3. inner join
The SQL statement is as follows:
Copy codeThe Code is as follows:
Select * from A innerjoin B on A. aID = B. bID
The result is as follows:
Copy codeThe Code is as follows:
AID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
Result description:
Obviously, only the records of A. aID = B. bID are displayed here. This indicates that inner join is not based on WHO, and only records meeting the conditions are displayed.
Note:
The left join operation is used to combine records of the source table in any FROM clause. Use the left join operation to create a LEFT Outer JOIN. The outer join on the left contains all records from the first (left) two tables, even if there is no record with consistent values in the second (right) table.
Syntax: select from table1 left join table2 ON table1.field1 compopr table2.field2
Note:
The table1 and table2 parameters are used to specify the names of the tables for record combination.
The field1 and field2 Parameters specify the names of joined fields. These fields must have the same data type and contain the same data type, but they do not need the same name.
The compopr parameter specifies the relational comparison operator: "=", "<", ">", "<=> =" or "<> ".
If you want to JOIN a field that contains the Memo data type or OLE Object data type in the inner join operation, an error will occur.
Therefore, in my understanding, SQL should be written as follows:
Select software from Software Table inner join device table on Software Table. mac = device table. mac where device table. pro = 'xxx' and device table. city = 'xxx ';