MySQL database left join query for three tables

Source: Internet
Author: User

Three table union queries we can use the following syntax

The code is as follows Copy Code

SELECT * from a LEFT join B to a.id=b.id left join C on b.id=c.id

Example

Now it's time to list all the users and the departments and positions they belong to.

Tb_user Users

ID sname Id_pos
1 Li Good 2
2 Good inside 3
3 Small outside 1

Tb_pos position

ID scaption ID_DPT
1 Manager 0
2 Program 1
3 1

TB_DPT Department

ID scaption
1 Design Department
2 Operation and Maintenance department

Because you want to list each record in the tb_user, we need to use left JOIN to answer the query. And there are 3 tables to connect, so it should be written

The code is as follows Copy Code

SELECT u.sname p.scaption d.scaption from Tb_user as U Left join (Tb_pos as P-left join TB_DPT as D on p.id_dpt=d.id) on U . id_pos=p.id;

Then after the run came out tb_user new table, the method is not very simple ah, here is also used in the left JOIN joint query.

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.