Mysql multi-table join _ MySQL

Source: Internet
Author: User
Mysql multi-table Association bitsCN.com

Mysql multi-table Association

Association of multiple tables is often used in databases. Mysql associations mainly include inner join, left join, and right join. The following describes and provides examples.
As the name suggests, inner join combines the information of two tables, and is associated only when all tables are included.
Left join is based on the first table. if the information of the last table is incomplete, it is NULL.
Right join is based on the second table. if the information of the previous table is incomplete, it is NULL.
In addition, it should be noted that a join condition may correspond to several pieces of information in the table, so that all the information needs to be associated.
Example:
Create the following three tables: article, us, type:







The following describes only the right association in detail. Other associations only provide statements and the final results.
First, right join of the first two tables

Java code select article. aid, article. title, us. username from article right join us on article. uid = us. uid;

Because it is associated, the us table prevails. If us. uid is 1, there are two results in article, but if us. uid is 3, it is not in article. Therefore, the final result is as follows:


Then, the right join type table is continued in the result.

Java code select article. aid, article. title, us. username, type. typename from article right join us on article. uid = us. uid right join type on article. tid = type. tid;

In this case, the type table prevails. for the above association results, in fact, aid1-> tid: 1, aid2-> tid: 2, aid3-> tid: 1, the final result is as follows:


For inner join and left join, similar to the above, we will introduce a simple association between two tables.





Note that the first table here is: us

BitsCN.com

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.