The Language of SQL- P29 doc

5 213 0
The Language of SQL- P29 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

are listed in a left or right join is significant. However, there is some flexibility in listing the tables in situations where there are three or more tables. The order of the left (or right) join keywords can be switched around if desired. Let’s again look at the original FROM clause from the previous SELECT: FROM Customers LEFT JOIN Orders ON Customers.CustomerID ¼ Orders.CustomerID LEFT JOIN Refunds ON Orders.OrderID ¼ Refunds.OrderID We’ve already seen that you can list the Refunds table first and the Customers table last, as long as you convert everything to right joins, as in: FROM Refunds RIGHT JOIN Orders ON Orders.OrderID ¼ Refunds.OrderID RIGHT JOIN Customers ON Customers.CustomerID ¼ Orders.CustomerID Is it possible to list the Customers table first and then the Refunds table, followed by the Orders table? Yes, as long as you’re willing to mix left and right joins together and throw in some parentheses. The following is equivalent to the above: FROM Customers LEFT JOIN (Refunds RIGHT JOIN Orders ON Orders.OrderID ¼ Refunds.OrderID) ON Customers.CustomerID ¼ Orders.CustomerID What was originally a fairly simple statement has now turned into something unnecessarily complex. Our advice is to stick with the LEFT JOIN keyword and avoid parentheses when devising complex FROM clauses with multiple tables. Full Joins The remaining outer join type is the full join. You’ve seen that in left and right joins, one table is primary and the other one is secondary. Alternatively, you can say that one table is required and one is optional, which means that when matching two tables, rows in the secondary (or optional) table don’t necessarily have to exist. Chapter 12 ■ Combining Tables with an Outer Join126 In the inner join, both tables are primary (or required). When matching two tables, there has to be a match between both tables for a row of data to be selected. In the full join, both tables are secondary (or optional). In this situation, if we’re matching rows in table A and table B, then we display 1) all rows from table A, even if there is no matching row in table B, and also 2) all rows from table B, even if there is no matching row in table A. DATABASE DIFFERENCES: MySQL Unlike Microsoft SQL Server and Oracle, MySQL doesn’t provide for a full join. Let’s look at an example involving matching rows from these two tables. First, a Movies table: MovieID MovieTitle Rating 1 Sleepless in Seattle PG 2 Lost in America R 3 Bambi G 4 North by Northwest Not Rated 5 Forrest Gump PG-13 6 The Truman Show PG Second, a Ratin gs table: RatingID Rating RatingDescription 1 G General Audiences 2 PG Parental Guidance Suggested 3 PG-13 Parents Strongly Cautioned 4 R Restricted 5 NC-17 No One 17 and Under Admitted The Movies table has a list of movies in the database and includes the MPAA rating for each movie. The Ratings table has a list of the ratings and their descriptions. Let’s say you want to find all matches between these two tables. You’re going to use a FULL JOIN to show all rows from the Movies table, as well Full Joins 127 as all rows from the Ratings table. The full join will show all rows, even if a match from the other table isn’t found. The SELECT looks like: SELECT MovieTitle AS 'Movie', RatingDescription AS 'Rating Description' FROM Movies FULL JOIN Ratings ON Movies.Rating ¼ Ratings.Rating ORDER BY RatingDescription, MovieTitle The result of this statement is: Movie Rating Description North by Northwest NULL Bambi General Audience NULL No One 17 and Under Admitted Sleepless in Seattle Parental Guidance Suggested The Truman Show Parental Guidance Suggested Forrest Gump Parents Strongly Cautioned Lost in America Restricted Notice that there are two blank cells in the data, which is a direct result of having used a FULL JOIN. In the first instance, there is no rating shown for North by Northwest because there was no matching row in the Ratings table for that movie. In the second instance, there is no movie shown for the ‘‘No One 17 and Under Admitted’’ rating description because there were no matching rows in the Movies table for that rating. The full join is seldom used in practice for the simple reason that this type of relationship between tables is relatively uncommon. In essence, the full join shows data where there are nonmatches in both directions between two tables. We are normally only interested in data where there is a complete match between two tables (the inner join) or perhaps a one-sided match (the left or right join). Looking Ahead This chapter extended our discussion of joins to left, right, and full joins. The left join enables you to join a prim ary and a secondary table together. The left join Chapter 12 ■ Combining Tables with an Outer Join128 shows all rows in the primary table, even if there is no match in the secondary table. The right join is simply the reverse of the left join, switching the order of the primary and secondary tables. Finally, the full join enables both tables to be secondary tables. It displays all rows that are in either table, even if there isn’t a match in the other table. In our next chapter, ‘‘Self Joins and Views,’’ we’re going to take a slight detour to two related topics. First, we’re going to talk about self joins, which is a special technique that allows us to join a table to itself. In a way, this creates a virtual view of the table, in the sense that we can now view this table from two different perspectives. The second main topic of the next chapter will extend the concept of self joins to a more general way of creating virtual views of multiple tables. Looking Ahead 129 This page intentionally left blank . if there is no match in the secondary table. The right join is simply the reverse of the left join, switching the order of the primary and secondary tables. Finally, the full join enables both. 17 and Under Admitted The Movies table has a list of movies in the database and includes the MPAA rating for each movie. The Ratings table has a list of the ratings and their descriptions. Let’s. creates a virtual view of the table, in the sense that we can now view this table from two different perspectives. The second main topic of the next chapter will extend the concept of self joins to

Ngày đăng: 05/07/2014, 05:20

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan