Ambiguous left joins in MS Access

独自空忆成欢 提交于 2019-12-01 23:37:33

问题


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)


来源:https://stackoverflow.com/questions/11734577/ambiguous-left-joins-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!