• Tidak ada hasil yang ditemukan

This is a result of the order in which the tables were added to the query grid. Equi-join query operations are associative in nature (recall your first year of algebra; it doesn’t matter if you add A+(B+C) or (A+B)+C, the result will be the same). So it doesn’t matter which tables you operate on first — the results will be identical.

The following script shows another method for writing the query:

SELECT T.*, P.ProductName, C.Firstname, C.Lastname FROM Products AS P, Customers AS C, Transactions AS T WHERE C.CustomerID = T.CustomerID AND

P.ProductID = T.ProductID;

This script simply lists all the tables in the FROM clause and then shows the relationship and condition in a WHERE clause.

I prefer this method since it is simpler to compose.

Right Outer Join

A right outer join selects every record from the table specified to the right of the RIGHT JOIN keywords. Take a look at Example 5.

Example 5

Suppose you want to query the Customers2 table shown in Fig- ure 8-19 and the Transactions table shown in Figure 8-20 to display customers and information about their purchases. Addi- tionally, you want to display customers on the mailing list who have not yet made any purchases.

Using Access Design view, this would be represented by the query shown in Figure 8-22.

Ü

Note: You modify the type of join by highlighting the join, clicking on the join line, right-clicking, selecting Join Properties, then specifying the join type in the Join Properties dialog (Fig- ure 8-21).

Figure 8-19. Customers2 table

Figure 8-20. Transactions table

After clicking on the OK button, the outer join is represented by the arrow joining the two tables, as shown in Figure 8-22.

Figure 8-21. Join Properties dialog

Figure 8-22. Query Design view

Converting to SQL view (Figure 8-23) shows the SQL query:

Putting in our usual aliases and ordering by the customer ID produces the following script:

SELECT C.CustomerID, C.Lastname, T.ProductID, T.DateSold FROM Transactions AS T RIGHT JOIN Customers2 AS C ON C.CustomerID = T.CustomerID

ORDER BY C.CustomerID;

The preceding script uses the RIGHT JOIN keywords in the FROM clause to instruct Microsoft Access to display all the records in the table (Customers2) specified to the right of the RIGHT JOIN keywords. Although the ON keyword specifies a condition to retrieve the customer IDs from the Customers2 table that are equal to a customer ID in the Transactions table, the RIGHT JOIN keywords cause the DBMS to display all the records from the Customers2 table, including those records that do not match with any customer ID in the Transactions table. Look at the results in Figure 8-24. Notice the customer IDs and names with no product IDs or dates. These customers have not made any purchases yet.

Figure 8-23. SQL view

Ü

Note: A term commonly used when dealing with joins is Cartesian product. ACartesian productexists when you create a join without the specification of a relationship between tables. A Cartesian product causes each row in one table to be multiplied by the total number of rows in another table. This is rarely the result sought after when creating a join. Be careful to always specify the relationship between joined tables.

Left Outer Join

A left outer join works much like a right outer join except it selects every record from the table specified to the left of the LEFT JOIN keywords. Take a look at Example 6.

Example 6

Suppose you want to query the Customers2 table shown in Fig- ure 8-19 and the Transactions table shown in Figure 8-20 to display customers and information about their purchases. Addi- tionally, you want to display customers on the mailing list who have not yet made any purchases. This is exactly what we did in Example 5, but this time we’ll use LEFT JOIN instead of RIGHT JOIN. Look at the following script:

SELECT C.CustomerID, C.Lastname, T.ProductID, T.DateSold FROM Customers2 AS C LEFT JOIN Transactions AS T ON C.CustomerID = T.CustomerID

ORDER BY C.CustomerID;

Figure 8-24. Results (output)

The preceding script is equivalent to the script in Example 5.

The LEFT JOIN keywords in the FROM clause are used to instruct Microsoft Access to display all the records in the table (Customers2) specified to the left of the LEFT JOIN keywords.

Look at the results in Figure 8-25. As you can see, the results are the same as the results for Example 5.

What does this look like in the Access query grid? Using the simplified version of the SQL query, we type the text into the SQL view as shown in Figure 8-26:

Then we convert this to Design view (Figure 8-27) and, sur- prise, we get the same query that we started with in the section on right joins. Access is somewhat arbitrary in that no matter how the tables are entered into the query grid, it will try to interpret the operation as a right join. In this respect, if you want more control of your joins, you will find that it is eas- ier to do them in SQL view. Several times I have wondered

Figure 8-25. Results (output)

Figure 8-26. SQL view

what exactly was being done by my Access queries. The answers became apparent when the query was converted to SQL.

Create a Join that Contains an Aggregate Function

In Chapter 7, you learned how to create queries that contain aggregate functions. Now let’s use an aggregate function while joining two tables. Take a look at Example 7.

Example 7

Figure 8-27. Query Design view

Figure 8-28. Customers table

Suppose you want to retrieve the customer names and the total number of transactions per customer. Look at the following script:

SELECT FirstName, LastName, COUNT (Transactions.CustomerID) AS TotalTransactions

FROM Customers, Transactions

WHERE Transactions.CustomerID = Customers.CustomerID GROUP BY FirstName, LastName;

The preceding script uses an aggregate (COUNT ()) function after the SELECT keyword. The FROM clause specifies two tables (Customers, Transactions). The WHERE clause shows the relationship between the Customers and the Transactions tables.

Whenever you use an aggregate function in a query, you also must use the GROUP BY clause. Recall from Chapter 7, all col- umn names retrieved from the database (specified after the SELECT keyword) must be present in the GROUP BY clause.

Take a look at the results in Figure 8-30.

Figure 8-29. Transactions table

Figure 8-30. Results (output)

Dokumen terkait