SQL left JOIN left join

Source: Internet
Author: User
Tags join

SQL LEFT JOIN LEFT JOIN this approach may facilitate the operation of multiple data tables, and we can achieve the left join effect we want with just the right join on.

MySQL left-side connection
In the last lesson, database connection, we learned how to do a basic join of the two tables. This lesson will teach you how to make a special addition: left to join.


MySQL left-link explanation
How is a left connection different from the normal join? First, the syntax is completely different and more complex. In addition to looking for a different, left connection provides extra consideration to the table on the left.

Being "left" simply refers to the table that appears on the left to join our SQL statement. Any tricky.

This extra consideration to the left table can be considered a special preserve. Each item on the left table will appear in a MySQL result, even if there is no match with the other table, it is joining.

MySQL joins and leaves to join the disagreement
The following are lessons from our table using previous database connections.

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

Left JOIN instance Www.111cn.net

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 the left-connected query, but all the family members are listed, even if they do not have favorite dishes in our food table.

This is because the left connection will keep the record "left" seated.

Example of the left connection of MySQL
The following code is exactly the same code in the past lesson, except that the left connection has been added to the query. Let's see if the result is what we expect

$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/>";
}

Results.

Dad-steak
Dad-tacos
Mom-salad
Daughter-
Dog-

All right, reprinted annotated Www.111cn.net/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.