Ambiguous left joins in MS Access

2019-02-25 11:51发布

问题:

I want to convert the following query from T-SQL

SELECT
    * 
FROM
    A                            LEFT JOIN
    B ON A.field1 = B.field1     LEFT JOIN
    C ON C.field1 = A.field2 AND
         C.field2 = B.field2

to Jet SQL.
Now MS Access does not accept ambiguous queries. How can I do that? I can't put the second comparison in the WHERE clause. Why? Because my scenario is that I am selecting records that does not exist in C.

How to select all records from one table that do not exist in another table?

Now, how do you that in MS Access? Thanks in advance for your time and expertise.

回答1:

You need a derived table to make this work in MS Access:

SELECT * 
FROM (
   SELECT A.Field1, A.Field2 As A2, B.Field2 
   FROM A 
   LEFT JOIN B ON A.field1 = B.field1) AS x 
LEFT JOIN C ON  x.A2 = C.field1 AND  x.field2= C.field2 


回答2:

From Help LEFT JOIN, RIGHT JOIN Operations

You can link multiple ON clauses. See the discussion of clause linking in the INNER JOIN topic to see how this is done.

You can also link several ON clauses in a JOIN statement, using the following syntax:

SELECT fields 
FROM table1 
     INNER JOIN table2 ON table1.field1 compopr table2.field1 
                          AND ON table1.field2 compopr table2.field2)
                          OR ON table1.field3 compopr table2.field3)];

But works this (it seems there is an error in help):

SELECT * 
FROM A 
     LEFT JOIN B ON A.field1 = B.field1
     LEFT JOIN C ON (C.field1 = A.field2 AND C.field2 = B.field2)