Left join, join, right join difference, leftjoin

Source: Internet
Author: User

Left join, join, right join difference, leftjoin
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:aID     aNum1     a200501112     a200501123     a200501134     a200501145     a20050115 Table B records the following:bID     bName1     20060324012     20060324023     20060324034     20060324048     2006032408 --------------------------------------------1.left joinThe SQL statement is as follows:select * from Aleft join B on A.aID = B.bID The result is as follows:aID     aNum     bID     bName1     a20050111    1     20060324012     a20050112    2     20060324023     a20050113    3     20060324034     a20050114    4     20060324045     a20050115    NULL     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 joinThe SQL statement is as follows:select * from Aright join B on A.aID = B.bID The result is as follows:aID     aNum     bID     bName1     a20050111    1     20060324012     a20050112    2     20060324023     a20050113    3     20060324034     a20050114    4     2006032404NULL     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 joinThe SQL statement is as follows:select * from Ainnerjoin B on A.aID = B.bID The result is as follows:aID     aNum     bID     bName1     a20050111    1     20060324012     a20050112    2     20060324023     a20050113    3     20060324034     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: 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 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.