Database Reference
In-Depth Information
Figure 7.12
Left join swapping the tables' positions.
Again by formatting the query in a line we can see how it works in a clearer way:
SELECT webpage.title, log.datecreated FROM log RIGHT JOIN webpage ON ….
This time it is the rightmost table, webpage , which has all of its rows returned, with only
the matching rows in the left table, log , in the result set.
If you have just worked through the previous examples, you will notice that running this
query produces the same result as the left join shown in Figure 7.11 when the table order is
reversed.
This shows an interesting function of the outer join. The following two scripts will pro-
duce identical result sets:
SELECT columns
FROM
firsttable
RIGHT JOIN secondtable ON
firsttable.column = secondtable.column
SELECT columns
FROM
secondtable
LEFT JOIN
firsttable ON
firsttable.column = secondtable.column
If you understand this, you will realize that there is not actually a need for a system to
implement both the left and right join, as you can accomplish both by the re-ordering of
table order within the query. MySQL implements both of these joins but some other SQL
systems only implement one of them. Though not necessary, the two types of join make
query building easier when joining more than two tables with outer joins.
Search WWH ::




Custom Search