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