Database Reference
In-Depth Information
Figure 7.10
All of the entries in the webpages table.
the Home page listed four times and the Links page once. We are only displaying the page
title for rows in our log table: the rows that match. Figure 7.10 will remind you of all of the
entries in the log table.
So we need to convert this equi-join into a left join to get the desired results. Execute the
following query:
SELECT webpage.title, log.datecreated
FROM
webpage
LEFT JOIN log ON
webpage.id = log.webpageid
Look at the results in Figure 7.11. You will see that we still have all of the rows that
appeared in Figure 7.9, with the addition of the extra rows in the webpage table. As there is
no corresponding entry in the last four rows, the MySQL server has returned a NULL for
the value in the datecreated column.
If we look at the query again, we can see why we call it a left join:
SELECT webpage.title, log.datecreated FROM webpage LEFT JOIN log ON ….
This has been formatted in a different way so that you can see that the webpage table is
on the left of the log table as it is written. All of the rows in the left table - the webpage table
- will be returned with the matching rows in the rightmost table. This is a good way to
remember which column does which in a left join. The leftmost column in the query will
return all its rows irrespective of matches in the right column.
Let us now swap the two columns around in the query, so that we make the log table the
leftmost:
SELECT webpage.title, log.datecreated
FROM
log
LEFT JOIN webpage ON
webpage.id = log.webpageid
Search WWH ::




Custom Search