0

当我尝试在 MS-Access 2010 中运行以下查询时,我在 FROM 子句对话框中收到语法错误:

SELECT 
    Contact_addresses.AddressID, 
    Contact_addresses.ContactID, 
    Contact_addresses.Address1, 
    Contact_addresses.Address2, 
    Contact_addresses.City, 
    Contact_addresses.State, 
    Contact_addresses.Zip, 
    Owner_Operator.FIRM_NAME, 
    Official_Correspondent.FIRST_NAME, 
    Official_Correspondent.LAST_NAME, 
    Official_Correspondent.SUBACCOUNT_COMPANY_NAME, 
    Official_Correspondent.PHONE_NUMBER
FROM Contact_addresses 
    (
        LEFT JOIN 
            (SELECT
                Owner_Operator.CONTACT_ID, 
                Owner_Operator.FIRM_NAME 
                FROM Owner_Operator) 
            ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
        ) 
        LEFT JOIN 
            (SELECT 
                Official_Correspondent.CONTACT_ID, 
                Official_Correspondent.FIRST_NAME, 
                Official_Correspondent.LAST_NAME, 
                Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
                Official_Correspondent.PHONE_NUMBER 
                FROM Official_Correspondent) 
            ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
        ;  

当我关闭对话框时,访问会突出显示(在 FROM Contact_addresses.

我知道我需要在 Access 中使用多个嵌套连接的括号,但是除了显示如何解决问题之外,有人可以解释一下它是如何工作的概念吗?

从 SQL SELECT 语句中应该清楚底层表架构的相关方面。

4

2 回答 2

1

我认为这应该可行,但我无法进行测试。要坚持的一般语法

From table left join (statement) alias on table.col = alias.col left join ...

将您的陈述更改为:

FROM Contact_addresses 

    LEFT JOIN 
        (SELECT
            Owner_Operator.CONTACT_ID, 
            Owner_Operator.FIRM_NAME 
            FROM Owner_Operator)   Owner_Operator
        ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID

    LEFT JOIN 
        (SELECT 
            Official_Correspondent.CONTACT_ID, 
            Official_Correspondent.FIRST_NAME, 
            Official_Correspondent.LAST_NAME, 
            Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
            Official_Correspondent.PHONE_NUMBER 
            FROM Official_Correspondent)  Official_Correspondent
        ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
    ;  

我添加了表别名以匹配您在联接中所称的名称,并删除了有问题的括号集。

于 2013-10-18T18:54:26.947 回答
0

问题出在左括号的位置((。它们需要紧跟在第一个 FROM 之后。这是有效的方法:

SELECT Contact_addresses.AddressID, Contact_addresses.ContactID, Contact_addresses.Address1, Contact_addresses.Address2, Contact_addresses.City, Contact_addresses.State, Contact_addresses.Zip, Owner_Operator.FIRM_NAME, Official_Correspondent.FIRST_NAME, Official_Correspondent.LAST_NAME, Official_Correspondent.SUBACCOUNT_COMPANY_NAME, Official_Correspondent.PHONE_NUMBER
FROM ((Contact_addresses 

        LEFT JOIN 
            (SELECT 
                Owner_Operator.CONTACT_ID, 
                Owner_Operator.FIRM_NAME 
                FROM Owner_Operator) AS Owner_Operator 
            ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID 
)
        LEFT JOIN 
            (SELECT 
                Official_Correspondent.CONTACT_ID, 
                Official_Correspondent.FIRST_NAME, 
                Official_Correspondent.LAST_NAME, 
                Official_Correspondent.SUBACCOUNT_COMPANY_NAME, 
                Official_Correspondent.PHONE_NUMBER 
                FROM Official_Correspondent) AS Official_Correspondent 
            ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID 
)        
;
于 2013-10-18T19:43:12.930 回答