MySQLJOIN multi-table join (4)

Source: Internet
Author: User
In addition to two commonly used table connections, the MySQLJOIN multi-table JOIN syntax also supports multi-table JOIN. The basic syntax for multi-table JOIN is as follows:... FROMtable1INNER | LEFT | RIGHTJOINtable2ONconditionINNER | LEFT | RIGHTJOINtable3ONcondition... JOIN multi-table JOIN achieves multi-table JOIN from multiple tables

In addition to two commonly used table connections, the SQL (MySQL) JOIN syntax also supports multi-Table connections. The basic syntax for multi-table join is as follows :... FROM table1 INNER | LEFT | right join table2 ON condition INNER | LEFT | right join table3 ON condition... JOIN multi-table JOIN enables

MySQL JOIN multi-table JOIN

In addition to the two commonly used table joins, the SQL (MySQL) JOIN syntax also supports multi-Table joins. The basic syntax for multi-table join is as follows:

... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON condition INNER|LEFT|RIGHT JOIN table3 ON condition ...

JOIN multi-table JOIN allows you to obtain relevant data from multiple tables. The following are three original data tables:

Articles:
Aid Title Content Uid Tid
1 Article 1 Article 1 body content... 1 1
2 Article 2 Article 2 body content... 1 2
3 Article 3 Article 3 body content... 2 1
5 Article 5 Article 5 body content... 4 1
User table:
Uid Username Email
1 Admin Admin@5idev.com
2 James Xiao@163.com
3 Jack Jack@gmail.com
Type document type table:
Tid Typename
1 Common articles
2 Excellent articles
3 Draft
MySQL inner join multi-table

We use inner join to list data associated with all three tables:

SELECT article.aid,article.title,user.username,type.typename FROM article INNER JOIN user ON article.uid=user.uid INNER JOIN type ON article.tid=type.tid

The returned query result is as follows:

Aid Title Username Typename
1 Article 1 Admin Common articles
2 Article 2 Admin Excellent articles
3 Article 3 James Common articles
MySQL left join multi-table

Use left join to query three tables:

SELECT article.aid,article.title,user.username,type.typename FROM article LEFT JOIN user ON article.uid=user.uid LEFT JOIN type ON article.tid=type.tid

The returned query result is as follows:

Aid Title Username Typename
1 Article 1 Admin Common articles
2 Article 2 Admin Excellent articles
3 Article 3 James Common articles
4 Article 4 NULL Common articles
MySQL right join multi-table

Use right join to query three tables:

SELECT article.aid,article.title,user.username,type.typename FROM article RIGHT JOIN user ON article.uid=user.uid RIGHT JOIN type ON article.tid=type.tid

The returned query result is as follows:

Aid Title Username Typename
1 Article 1 Admin Common articles
2 Article 2 Admin Excellent articles
3 Article 3 James Common articles
NULL NULL NULL Draft

It can be seen that in right join, only all data of the last right join table is listed.

Description

For MySQL multi-table JOIN, you can also mix INNER, LEFT, and RIGHT. The returned results are related to the order of keywords. If you are interested, you can test the results by yourself.


From http://www.5idev.com/p-php_mysql_join_multi_table.shtml

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.