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.
Position |
| Age
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