MySQL LEFT JOIN query Association n multiple Tables

Source: Internet
Author: User

The left join is a left table datum, 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  Left Join Table  on = 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  Left Join table B (left jointablein= on = 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.

SELECTGroup_concat (U.stuffname), Group_concat (ee.id), Ee.applytime, P.orderstate, Group_concat (conc At (S.departureairportcode,'-', S.landingairportcode)), P.PNR, SNS.C, sns.b, SNS.A, Ee.pnr NEWP, Group_conc At (CONCAT (s.departuredate,' ', S.departuretime)), Group_concat (S.flightnum), Group_concat (S.seatclass), C.rebookint Sumfee, C.customerrebookintfee, c.amountreceivable, O.ordernum, Comp.companycode, Comp.companyname fromendrose ee Left JOINPassenger P onEe.passengerid=p.id Left JOINSegment S onS.endroseid=ee.id Left JOINCostfee C onC.endroseid=ee.id Left JOIN(    SELECTNs.passengerid, Group_concat (ns.flightnum) b, Group_concat (Ns.seatclass) A, Group_concat ( CONCAT (Ns.departuredate,' ', Ns.landingdate)) C fromsegment NSWHEREns.orderstate= 0    GROUP  byNs.passengerid) SNS onSns.passengerid=Ee.passengerid Left JOIN(Passenger passe Left JOINAirticketorder o onPasse.orderid=o.id) onPasse.id=Ee.passengerid Left JOIN(Passenger pass Left JOIN`User' U onPass.stuffuuid=u.id) onPass.id=Ee.passengerid Left JOIN(Passenger Passen Left JOIN(Airticketorder ao Left Join(            `User' UA Left JoinCompany Comp onComp.id=Ua.companyid) onAo.bookerstuffid=ua.id) onPassen.orderid=ao.id) onPassen.id=Ee.passengeridGroup  byEe.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.


Http://www.cnblogs.com/amyStart/p/5965472.html




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.