Вы находитесь на странице: 1из 2

LEFT OUTER JOIN The outer table is determined by its location in the FROM clause of the SELECT a s shown

here: <Outer-table> LEFT OUTER JOIN <Inner-table> Or <Outer-table> LEFT JOIN <Inner-table> In this format, the Customer table is the one on the left of the word JOIN. Sinc e this is a LEFT OUTER JOIN, the Customer is the outer table. This syntax can return all cus tomer rows that match a valid order number (INNER JOIN) and Customers with NULL or inv alid order numbers (OUTER JOIN). The next SELECT shows customers with matching orders and those that need to be c alled because they have not placed an order: SELECT Customer_name ,Order_number ,Order_total (format '$$$,$$9.99-' ) FROM Customer_table cust LEFT OUTER JOIN Order_table ord ON cust.customer_number = ord.customer_number ORDER BY 1 ; 6 Rows Returned Customer_name Order_number Order_total Ace Consulting 123552 $5,111.47 Acme Products ? ? Billy's Best Choice 123456 $12,347.53 Billy's Best Choice 123512 $8,005.91 Databases N-U 123585 $15,231.62 XYZ Plumbing 123777 $23,454.84 The above output consists of all the rows from the Customer table because it is the outer table and there are no residual conditions. Unlike the earlier INNER JOIN, Acme Products is now easily seen as the only customer without an order. Since Acme Products has n o order at this time, the order number and the order total are both extended with the "? " to represent a NULL, or missing value from a non-matching row of the inner table. This is a v ery important concept. The result of the SELECT provides the matching rows like the INNER JOIN and the nonmatching rows, or exceptions that are missed by the INNER JOIN. It is possible to add the order number to an ORDER BY to put all exceptions either at the front (ASC) or b ack (DESC) of the output report. When using an OUTER JOIN, the results of this join are stored in the spool area and contain all of the rows from the outer table. This includes the rows that match and all the rows that do not match from the join step. The only difference is that the non-matching ro ws are carrying the NULL values for all columns for missing rows from the inner table. The concept of a LEFT OUTER JOIN is pretty straight forward with two tables. How ever, additional thought is required when using more then two tables to preserve rows from the

first outer table. Remember that the result of the first join is saved in spool. This same spool is then used to perform all subsequent joins against any additional tables, or other spool areas . So if you join 3 tables using an outer join the first two tables are joined together with the spooled results representing the new outer table and then joined with the third table wh ich becomes the RIGHT table. Using the Student, Course and Student_Course tables, the following SELECT preser ves the exception rows from the Student table as the outer table, throughout the entire join. Since both joins are written using the LEFT OUTER JOIN and the Student table is the ta ble name that is the furthest to the left it remains as the outer table: SELECT Last_name (Title 'Last Name') ,First_name AS First ,S.Student_ID ,Course_name AS Course FROM Student_table AS S LEFT OUTER JOIN Student_Course_table AS SC ON S.student_id = SC.student_id LEFT OUTER JOIN Course_table AS C ON C.course_id = SC.course_id ORDER BY Course, Last_name ;

Вам также может понравиться