PRB: Using "!=" in a Join Statement Does Not Return RecordsLast reviewed: November 24, 1997Article ID: Q176888 |
The information in this article applies to:
SYMPTOMSWhen you specify a JOIN condition in a SQL Select statement using the ON clause, and you use the not equal sign (!=) with one empty table, the statement does not return any records.
STATUSThis behavior is by design.
MORE INFORMATION
ON Clause with !=The following syntax does not return all the records from one table that do not have a matching record in the other table:
SELECT * FROM tbl1 INNER JOIN tbl2 ON tbl1.fld1 != tbl2.fld1This creates a Cartesian product that excludes records that do match.
Where Clause with !=To create a result set that includes all records from one table that do not exist in a second table, use the following syntax:
SELECT * FROM tbl1 WHERE tbl1.fld1 != tbl2.fld1In conclusion, the following statements are not equivalent:
SELECT * FROM tbl1 inner join tbl2 on tbl1.fld1 != tbl2.fld1 SELECT * FROM tbl1 WHERE tbl1.fld1 != tbl2.fld1 Steps to Reproduce BehaviorPlace the following code in a program file and run the code. Note that there are no records returned in the cursor.
CREATE DATABASE MyTest CREATE TABLE tbl1 (fld1 c(10),fld2 c(20)) CREATE TABLE tbl2 (fld1 c(10), fld2 c(20)) INSERT INTO tbl1 VALUES('1','Rec1') INSERT INTO tbl1 VALUES ('2','Rec2') SELECT * INTO CURSOR MyCurse FROM tbl1 inner JOIN tbl2 ON ; tbl1.fld1 != tbl2.fld1For a demonstration of the Cartesian product, run the following code with the database created in the code above open:
INSERT INTO tbl2 VALUES ('1','tbl2Rec1') INSERT INTO tbl2 VALUES ('2','tbl2Rec2') SELECT * INTO CURSOR MyCurse FROM tbl1 inner JOIN tbl2 ON ; tbl1.fld1 != tbl2.fld1 REFERENCESFor additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q124589 TITLE : How to Create a Left-Right Outer Join in FoxPro for Windows |
Additional query words: SQL Select
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |