The information in this article applies to:
SUMMARYA self-join is a special form of query involving a relationship that a table has with itself. Because the field names and table name in a self- join are identical, you must specify them in an unambiguous way in the body of the query. In order to construct the query, the table name must be aliased, and the fields or columns may be aliased as well. MORE INFORMATION
A self-join is a query that requires two copies of a single table for its
result. The NWIND.MDB sample database supplied with Microsoft Access
version 2.0 is used as an example.
Examine all possible pairs of rows in Employees -- one from the first copy and one from the second copy. Then retrieve the names of both the employee and his or her manager if and only if the value in the Reports To field in one copy matches that of the Employee ID field in the second copy.To implement this logic, you need to reference two rows from the Employees table at the same time. To distinguish between the two references, you need to introduce arbitrary range variables, such as Employees_1 and Employees_2, over the Employees table. At any particular point, Employees_1 represents some row in the first copy of Employees, and Employees_2 represents some row from the second copy. The query, in the Microsoft Access dialect of SQL is:
Or you could have simply aliased only the second or duplicate copy of the
Employees table:
Also, you don't really need to alias the columns or fields returned from
the second copy of the table to disambiguate those fields from those in the
first copy, because the use of the As <aliasname> on the table name does
that for you. However, to make the results more meaningful, it is helpful
to alias the field or column names as well.
In other words, the following will also work:
Visual Basic Code ExampleThe following Visual Basic code demonstrates this:
REFERENCESThis reference is from the BIBLIO.MDB database: "Using SQL," 1990 Groff, James R., Osborne McGraw-Hill, Berkeley, CA Additional query words: 3.00
Keywords : |
Last Reviewed: January 20, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |