MySQL LEFT JOIN query Association n multiple Tables

Source: Internet
Author: User
Tags joins

The EFT Join left connection is based on left table, showing all the coordinates of the row, the right table and the left table associated with the data will be displayed, not associated is not displayed. The keyword is left join on.
* * Basic usage is as follows:
Select Table A left joins table B on a.id = b.ta_id**

Note: 1?? Where on the associated field should be the same field (the foreign key associated with the two tables)
2?? Because of the left table as the benchmark, the left table a record if the corresponding right table more than one record, the data found in the right table is displayed only one, if you want to display, you can use Group_contact () to separate the field with a comma display on a record. So the right table no matter how many, if and the left table are a pair of relations, then no problem, there is a one-to-many relationship, it is necessary to deal with.

The three table associations are as follows:
Select Table A left joins table B (left joins table C on b.id = c.tb_id) on a.id = B_ta.id

Further expansion, if the Association of seven or eight tables, like the following SQL, the usage is nothing more than a three-table association, a layer of layers, but the relationship to straighten out good.

SELECT Group_concat (U. Stuffname), Group_concat (EE. ID), EE. Applytime, P. Orderstate, Group_concat (CONCAT (s. Departureairportcode,'-', s. Landingairportcode)), p. PNR, SNS. C, SNS. B, SNS. A, EE. PNR NEWP, Group_concat (CONCAT (s. Departuredate,", S. Departuretime)), Group_concat (s. Flightnum), Group_concat (s. seatclass), C. Rebookintsumfee, C. Customerrebookintfee, C. amountreceivable, O. Ordernum, Comp. Companycode, Comp. Companynamefrom endrose eeleft JOIN passenger p on EE. Passengerid = P. idleft JOIN segment S on s. Endroseid = EE. idleft JOIN Costfee C on C. Endroseid = EE. Idleft JOIN (SELECT NS. Passengerid, Group_concat (NS. Flightnum) B, Group_concat (NS. Seatclass) A, Group_concat (CONCAT (NS. Departuredate,", NS. landingdate)) c from segment NS WHERE NS. orderstate =0 GROUP by NS. Passengerid) SNS on SNS. Passengerid = EE. Passengeridleft join (passenger passe left join Airticketorder o on passe. orderId = O.id) On Passe.id = Ee.passengeridleft JOIN (Passenger Pass left JOIN ' user ' U on pass.stuffuuid = U.id) on Pass.id = Ee.passengerid left JOIN (passenger Passen Left join (Airticketorder AO left join (' user ' UA left JOIN company comp on Comp.id = ua.companyid) on Ao.bookerstuffid = Ua.id) on Passen.orderid = Ao.id) On Passen.id = Ee.passengeridgroup by Ee.applytime            

This SQL, especially the last left join, has several tables associated with it, so you can straighten it out.
Write the time from the outer layer to write, a layer of left join, it is not easy to go wrong.

MySQL LEFT JOIN query Association n multiple Tables

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.