Outer Join Syntax Example for SELECT-SQL StatementLast reviewed: June 1, 1996Article ID: Q89181 |
The information in this article applies to:
SUMMARYWhen FoxPro Relational Query By Example (RQBE) is used to join two databases, a record in the parent database that does not have any corresponding record in the child database is omitted from the joined database. However, under some circumstances, this is not the desired behavior. The text below details how to perform an "outer join" that preserves all records in the parent database. NOTE: The RQBE interface cannot perform an outer join; a program must perform the join because the RQBE interface cannot process the necessary nested SELECT-SQL statements.
MORE INFORMATIONThe code below demonstrates an outer join of the CUSTOMER.DBF and INVOICES.DBF databases in the FOXPRO2\TUTORIAL directory. NOTE: In Visual FoxPro for Windows, open the CUSTOMER.DBF and ORDERS.DBF files in the VFP\SAMPLES\MAINSAMP\DATA subdirectory. Then, substitute CUSTOMER.COMPANY_NAME for CUSTOMER.COMPANY, CUSTOMER.CUSTOMER_ID for CUSTOMER.CNO, and ORDERS.ORDER_ID for INVOICES.INO in the code below. (Note that the space before each semicolon [;] character is required. Omitting the space from the query causes an error.)
SELECT customer.company, customer.cno, invoices.ino ; FROM customer, invoices ; WHERE customer.cno = invoices.cno ; UNION ; SELECT customer.company, customer.cno, 0 ; FROM customer ; WHERE customer.cno NOT IN ; (SELECT invoices.cno FROM invoices ; WHERE customer.cno = invoices.cno) ; INTO CURSOR QUERYThe lines from the first SELECT statement to just before the UNION statement
SELECT customer.company, customer.cno, invoices.ino ; FROM customer, invoices ; WHERE customer.cno = invoices.cno ;select all the records in the parent database that have a corresponding record in the child database. This is what you normally get when you do a join. The UNION statement tells FoxPro to include the output of the first half of this SELECT statement with the second half in the final results. It is important that the data in the second half of the query have the same structure as the data in the first half of the query, otherwise errors are generated. The lines after the UNION statement
SELECT customer.company, customer.cno, 0 ; FROM customer ; WHERE customer.cno NOT IN ; (SELECT invoices.cno FROM invoices ; WHERE customer.cno = invoices.cno) ;retrieve all of the records in the parent database that have no corresponding child record. This is done as follows:
ORDER BY cnoresults in an "Fieldname is not unique and must be qualified" error message. Similarly, the following clause
ORDER BY customer.cnoresults in an "SQL Invalid Order by" error message. To eliminate these errors, use the ORDER BY clause with a column number. For example, the following clause
ORDER BY 1results in a query ordered by CNO since CNO is in column 1 of the output table.
|
Additional reference words: VFoxMac 3.00b FoxDos VFoxWin FoxWin 2.00 2.50
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |