The following articles mainly describe the usage of MySQL left join, right join, and inner join) detailed analysis of the following articles is mainly to explain the actual operations in the form of an instance demonstration, the following is the detailed description of the article.
The following is an example
Table A records the following:
AID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
Table B records the following:
BID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
The SQL statements for creating these two tables are as follows:
- CREATE TABLE a
- aID int( 1 ) AUTO_INCREMENT PRIMARY KEY ,
- aNum char( 20 )
- )
- CREATE TABLE b(
- bID int( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
- bName char( 20 )
- )
- INSERT INTO a
- VALUES ( 1, ‘a20050111′ ) , ( 2, ‘a20050112′ ) , ( 3, ‘a20050113′ ) , ( 4, ‘a20050114′ ) , ( 5, ‘a20050115′ ) ;
- INSERT INTO b
- VALUES ( 1, ‘ 2006032401′ ) , ( 2, ‘2006032402′ ) , ( 3, ‘2006032403′ ) , ( 4, ‘2006032404′ ) , ( 8, ‘2006032408′ ) ;
The experiment is as follows:
1. MySQL left join (left join)
The SQL statement is as follows:
SELECT * FROM
Left join B
ON a. aID = B. bID
The result 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:
MySQL 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 (right join)
The SQL statement is as follows:
- SELECT * FROM a
- RIGHT JOING b
- ON a.aID = b.bID
The result 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 is the opposite to that of MySQL left join. This time, the right table (B) is used as the basis. If table A is insufficient, it is filled with NULL.
3. inner join (equal join or inner join)
The SQL statement is as follows:
SELECT * FROM
Inner join B
ON a. aID = B. bID
Equivalent to the following SQL statement:
SELECT *
FROM a, B
WHERE a. aID = B. bID
The result 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.
The MySQL left join operation is used in any FROM clause,
Records of the combined source table. Use the left join operation to create a LEFT Outer JOIN. The outer join on the left will contain all records in the two tables starting from the first left), that is
In the second right) table, there is no record of consistent values.
Syntax: FROM table1 left join table2 ON table1.field1 compopr table2.field2
Description: The table1 and table2 parameters are used to specify the names of the tables to be combined.
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 to have the same
Name.
The compopr parameter specifies the relational comparison operators: "=", "<", ">", "<=", "> =", 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.