In MySQL, a table left JOIN is associated with two table statements

Source: Internet
Author: User

First look at the example

The code is as follows Copy Code

SELECT P.price, Pd.name, m.manufacturers_name from (Products p
Left JOIN products_description pd on p.products_id = pd.products_id)
Left JOIN manufacturers m on p.manufacturers_id = m.manufacturers_id


A reminder of the "a left JOIN B on condition expression"

The on condition (on in a left JOIN B in condition expression) is used to determine how to retrieve rows of data from table B.

If no row of data in table B matches the condition on, a row of all columns of NULL data is generated

The conditions in the match phase WHERE clause are not used. The WHERE clause condition is used only after the match phase completes. It retrieves the filter from the data generated during the match phase.


Efficiency analysis of joint query

User table:

ID | Name
---------
1 | Libk
2 | Zyfon
3 | Daodao

User_action table:

user_id | Action
---------------
1 | Jump
1 | Kick
1 | Jump
2 | Run
4 | Swim

Sql:

The code is as follows Copy Code
Select ID, name, action from user as U
Left join user_action A on u.id = a.user_id

Result
ID | name | Action
--------------------------------
1 | LIBK | Jump①
1 | LIBK | Kick②
1 | LIBK | Jump③
2 | Zyfon | Run④
3 | Daodao | Null⑤

Analysis:
Note that there is also a user_id=4, Action=swim record in the user_action, but not in the results,
Id=3 in the user table, Name=daodao users do not have a corresponding record in user_action, but they appear in the result set
Because now is the left join, all work is left.
Results 1,2,3,4 are both in the left table and on the right table record, 5 is only on the left table, not on the right table record


Conclusion:
we can imagine that the left join works like this.
Read one from the left table and select all the right table records (n bars) to match on to form N records (including duplicate rows, such as: result 1 and result 3),
If there is no table on the right that matches the on condition, the connected fields are null.
Then read on to the next one.

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.