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.