I. Let's take a look at some of the simplest examples.
Example
Table
Aid adate
1 A1
2 A2
3 A3
Tableb
Bid bdate
1 B1
2 B2
4 B4
Two tables A and B are connected. fields with the same ID must be retrieved.
Select * from a inner join B on A. Aid = B. Bid this is only used to retrieve matching data.
In this case, the following information is taken:
1 A1 B1
2 A2 B2
Then left join refers:
Select * from a left join B on A. Aid = B. Bid
First, retrieve all the data in Table A, and then add the data that matches table A and table B.
In this case, the following information is taken:
1 A1 B1
2 A2 B2
3 A3 null characters
Right join is also available.
This means that all data in Table B is retrieved first, and then the data matching A and B is added.
In this case, the following information is taken:
1 A1 B1
2 A2 B2
4 blank characters B4
Left join or left Outer Join.
The result set of the left Outer Join includes all rows in the left table specified in the left outer clause, not just the rows matched by the join column. If a row in the left table does not match a row in the right table, all the selected list columns in the right table in the row of the associated result set are null.
2. Left join/right join/inner Join Operation demonstration
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 experiment is as follows:
1. 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:
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:
Select * from
Right 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
Null 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:
Select * from
Innerjoin 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
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 following are some online materials] ----------------
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:
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 whose records are 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 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.
Iii. Complex explanations and Examples
Introduction: External join and self-join inner join (equivalent join) only return rows with equal join fields in two tables left join (left join) returns the record right join (right join) that includes all records in the left table and joined fields in the right table) returns the equal sign (=) expression of the joined Field and Its Relation between the specified table on which all records in the right table are equal to the joined field in the left table. returns true or false. if the expression returns true, the query contains the record .! An external connection can only operate on data that already exists in the database.
Update (ctarticle as a left join ctclass as C on A. classid = C. classid) left join cttag as B on A. ArticleID = B. ArticleID
Set tag = tag + '', B. ArticleID = A. ArticleID, B. classid = A. classid, B. nclassid = A. nclassid
Where a. classid = 23 and A. nclassid = 0 and tagid is not null
Update (ctarticle as a left join (ctnclass as C left join ctclass as D on C. classid = D. classid) on. nclassid = C. nclassid and. classid = C. classid) left join cttag as B on. articleID = B. articleID set tag = D. class + ''+ C. nclass, B. articleID =. articleID, B. classid =. classid, B. nclassid =. nclassid where. classid = 23 and. nclassid= 197;
Update operation
Filter data in left join
Insert into cttag (ArticleID, classid, nclassid) Select. articleID,. classid,. nclassid from ctarticle a left join cttag B on. articleID = B. articleID where B. articleID is null
// This statement function is used to display all the content of the master table and insert the data that is not in the secondary table.
// The main function is to reduce data redundancy.
Continuation in the previous example
Select. *, B. *, C. *, D. * From cttag as D left join (ctarticle as a left join ctclass as B on. classid = B. classid) left join ctnclass as C on. nclassid = C. nclassid) on D. articleID =. articleID;
DisplayArticleAll in the table, calling the columns in the category table
Select. *, B. *, C. * From (ctarticle a left join ctclass B on. classid = B. classid) left join ctnclass C on. nclassid = C. nclassid
// Function. Sometimes, an article table contains data that is not present in an individual category table. You can use this syntax to read all the data in the article table.
// A is the document table, B is the main category, and C is the subcategory.
In the same example, add a space when appending data.
Insert into cttag (ArticleID, classid, nclassid, tag) Select a. ArticleID, A. classid, A. nclassid, D. Class + ''+ C. nclass
From (ctarticle as a left join (ctnclass C left join ctclass D on C. classid = D. classid) on. classid = C. classid and. nclassid = C. nclassid) left join cttag as B on. articleID = B. articleID where. classid = 4 and. nclassid= 154;
Connect n tables and append data to one of them, n = 4
Insert into cttag (ArticleID, classid, nclassid, tag) Select a. ArticleID, A. classid, A. nclassid, D. Class + C. nclass
From (ctarticle as a left join (ctnclass C left join ctclass D on C. classid = D. classid) on. classid = C. classid and. nclassid = C. nclassid) left join cttag as B on. articleID = B. articleID where. classid = 1 and. nclassid = 1;
// Explanation
Insert to table 2 (column 1, column 2, column 3, column 4)
Select alias A. Column 1, alias A. Column 2, alias A. Column 3, alias D. column 4, alias C. Column 5
From (Table 1 alias A left join (Table 3 alias C left join Table 4 alias D in alias C. column 2 equals to alias D. column 2) in alias. column 2 equals to alias C. column 2 and alias. column 3 = alias C. column 3) join table 2 on the left. Alias B is in alias. column 1 equals to alias B. column 1 is alias. column 2 = 1 and alias. column 3 = 1
Connect two tables and append data to one of them.
Insert into cttag (ArticleID, classid, nclassid)
Select a. ArticleID, A. classid, A. nclassid
From ctarticle as a left join cttag as B on A. ArticleID = B. ArticleID where a. classid = 1 and A. nclassid = 1;
// Explanation
Insert to table 2 (column 1, column 2, column 3)
Select alias A. Column 1, alias A. Column 2, alias A. Column 3
From table 1 alias A left join Table 2 alias B in alias A. Column 1 equals alias B. Column 1 where alias A. Column 4 = 1 and alias A. Column 5 = 1
Left join
Synchronize data from two tables
Update ctarticle a inner join cttag B on A. ArticleID = B. ArticleID Set B. classid = A. classid, B. nclassid = A. nclassid;
// Explanation
Update Table 1 alias A join Table 2 alias 2 in alias. column 1 equals to alias B. column 1 sets the alias B. column 2 is updated to alias. column 2, alias B. column 3 is updated to alias. column 3
Outer right connection
Select a. *, B. * From bunclass a right join ctclass B on A. classid = B. classid where a. nclassid = 20
Query aliases A and B, and only match the content in table B.
Add data to a connection table
Insert into cttag (TAG, ArticleID) Select top 1 B. tag,. articleID from ctarticle as a left join cttag as B on. articleID = B. articleID where. articleID order by. articleID DESC;
Work und 2
Insert into bureply
Select B. *, A. classid, A. nclassid
From article as a inner join reply as B on A. ArticleID = B. ArticleID
Where classid = 50;
Flexibility in practical application
Insert into butag (TAG, ArticleID, classid, nclassid)
Select B. Tag, A. ArticleID, A. classid, A. nclassid
From article as a inner join tag as B on A. ArticleID = B. ArticleID
Where classid = 24;
Add data to other tables
Insert into butag (TAG, ArticleID)
Select B. Tag, A. ArticleID
From article as a inner join tag as B on A. ArticleID = B. ArticleID
Where a. ArticleID <> false;
// Explanation
Add to receiving table (column 1, column 2)
Select alias B. Column 1, alias A. Column 2
From table 1 Table Name a join Table 2 table name B in alias A. Column C equals alias B. Column C
Where is the alias? A. Column C is not equal to no
Flexibility in practical application
Select B. Tag, A. ArticleID, A. classid, A. nclassid
From article as a inner join tag as B on A. ArticleID = B. ArticleID
Where a. classid = 24;
Query
Select B. Tag, A. ArticleID
From article as a inner join tag as B on A. ArticleID = B. ArticleID
Where a. ArticleID <> false;
// Explanation
Select alias B. Column, alias A. Column
From table 1 alias A join Table 2 alias B in alias A. Column C = alias B. Column C
Where is the alias? A. Column C is not equal to no
Note: As is not necessary