Because relational databases are more complexly structured, they sometimes require special query statements to retrieve the information you need most. For example, if you wanted to know what categories the www.php.net URL is filed under, you would need to find its url_id from the urls table, use that to retrieve all the url_category_ids from the url_associations table, and then retrieve the actual category values from the url_categories table for those url_category_ids. As you can see, this requires several steps and queries. By using a joinan SQL query performed by cross-referencing tablesyou can accomplish all of that in one fell swoop. Several types of joins are conceivable according to SQL. Beginning to intermediate users will find that the two most basic joins, which I'll teach in this chapter, will suffice for almost every application. The most used join is called an inner join. SELECT * FROM url_associations, url_categories WHERE url_associations. url_category_id = url_categories. url_category_id The benefit of this join is that it will retrieve all of the information from both the url_associations and url_categories tables wherever a url_associations.url_category_id is the same as the url_categories.url_category_id (Figure 5.17). In other words, the query will replace the url_category_id foreign key in the url_associations table with all of the information for that type from the url_categories table. An inner join like this will return records only where a match is made (so if a URL category were not being used by an existing URL, it would not be displayed). Figure 5.17. This join returns every column from both tables where the url_category_id values are equal.When selecting from multiple tables and columns, you must use the dot syntax (table.column) if they have columns with the same name. This is normally the case when dealing with relational databases because a primary key from one table will have the same name as a foreign key in another. If you are not explicit when referencing your columns, you'll get an error (Figure 5.18). Figure 5.18. Generically referring to a column present in multiple tables will cause an ambiguity error.The second type of join I'll discussan outer or left joindiffers from an inner join in that it could return records not matched by a conditional. An example of a left join is SELECT * FROM url_categories LEFT JOIN url_associations ON url_associations. url_category_id = url_categories. url_category_id Note that the comma from the inner join is replaced by the words LEFT JOIN and the word WHERE is replaced with ON. The most important consideration with left joins is which table gets named first. In this example, all of the url_categories records will be returned along with all of the url_associations information, if a match is made. If no url_associations match exists for a url_categories row, then NULL values will be returned instead (Figure 5.19). Figure 5.19. This left join returns more records than the comparative inner join (see Figure 5.17).If both tables in a left join have the same column name, you can simplify your query with SELECT * FROM url_categories LEFT JOIN url_associations USING (url_category_id) Because of the complicated syntax with joins, the SQL concept of an aliasintroduced in the Chapter 4will come in handy when writing them. To use joins
Tips
|