SQL LEFT JOIN

Source: Internet
Author: User

SQL LEFT JOIN: This method may facilitate operations between multiple data tables. We only need to use left join on to achieve the desired LEFT JOIN effect.

SQL LEFT JOIN may facilitate operations between multiple data tables.Left joinOn can achieve our desired left-connected effect.

MySQL left join
In the last lesson, we learned how to create a basic two-table join database connection. This course will teach you how to add a special member: add on the left.


MySQL left join explanation
How is a left join different from a normal join? First, the syntax is completely different and more complex. Apart from looking for differences, left join provides additional considerations that the table is on the left.

The "Left" only refers to the table, and the SQL statement is added to the left. Any tricky.

This extra consideration is that the Left table can be considered as a special save. The table on the left of each project appears in a MySQL result, even if there is no competition with other tables, it is joining.

Differences between joining and leaving MySQL
The following are lessons learned from using previous database connections in our table.

Position Age
Dad 41
Mom 45
Daughter 17
Dog  
Meal Position
Steak Dad
Salad Mom
Spinach Soup  
Tacos Dad

Left connected instance www.111cn.cn

SELECT food. Meal, family. Position
FROM family, food
WHERE food. Position = family. Position

Output.

Dad-Steak
Mom-Salad
Dad-Tacos

When we decide to use left join queries, but all the family member columns, even if they don't have the favorite dishes to sit at our grain.

This is because the left join will keep the recorded "Left" seat.

Example of left-side MySQL connection
The following code is exactly the same in the past, except that the left join has been added to the query. Let's take a look. If the result is our expectation

$ Query = "SELECT family. Position, food. Meal ".
"FROM family left join food ".
"ON family. Position = food. Position ";

$ Result = mysql_query ($ query) or die (mysql_error ());


// Print out the contents of each row into a table
While ($ row = mysql_fetch_array ($ result )){
Echo $ row ['position']. "-". $ row ['meal'];
Echo "<br/> ";
}

Result.

Dad-Steak
Dad-Tacos
Mom-Salad
Daughter-
Dog-

Now, reprinted www.111cn.cn/database/database.html

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.