MySQL Join detailed

Source: Internet
Author: User

The following is an example analysis of Table A recorded below: AID anum1a200501112a200501123a200501134a200501145a20050115 table B records as follows:BID bname1 2006032401 2 2006032402 3 2006032403 4 2006032404 8 2006032408create these two table SQL statements as follows: CREATE TABLE a AID int (1) auto_increment PRIMARYKEY,Anum Char (20) CREATE TABLE B (BID int ) (1) notNULLAuto_increment PRIMARYKEY,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.leftJoinThe SQL statement (left join) is as follows:SELECT*From a leftJOINB on a. AID =b.BID results are as follows:AID anum BID bname1 a20050111 1 2006032401 2 a20050112 2 2006032402 3 a20050113 3 2006032403 4 a20050114 4 2006032404 5 A2 0050115NULL       NULL(the number of rows affected is5line) Result Description:The left join is based on the records of Table a, a can be regarded as the left table, B can be regarded as the right table, and the leftmost join is left table.In other words, the record of the left table (A) will be fully represented, and the right table (B) will only display records that match the search criteria (in the example: A.aid = b.bid).NULL in places where the B table is not recorded enough. 2.rightJoinThe SQL statement (right join) is as follows:SELECT*From a right joing B on a. AID = B.BID results are as follows:AID anum BID bname1 a20050111 1 2006032401 2 a20050112 2 2006032402 3 a20050113 3 2006032403 4 a20050114 4 2006032404NULL    NULL8 2006032408(the number of rows affected is5line) Result Description:Take a closer look., you will find that the result of the left join is exactly the opposite, this time is based on the right table (B), where a is not sufficient to fill with null. 3.innerJoinSQL statements (equal joins or inner joins) are as follows:SELECT*From a INNERJOINB on a. AID =b.BID is equivalent to the following SQL sentence:SELECT*From a,b WHERE a. AID = B.BID results are as follows:AID anum BID bname1 a20050111 1 2006032401 2 a20050112 2 2006032402 3 a20050113 3 2006032403 4 a20050114 4 2006032404Result Description:it's obvious ., this shows only the records of A.aid = B.bid. This shows that inner join is not based on who, it only shows records that match the criteria.

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.