ms访问中嵌套连接中from子句中的语法错误

时间:2013-10-18 18:41:19

标签: sql ms-access

当我尝试在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语句中可以清楚地了解基础表的模式的相关方面。

2 个答案:

答案 0 :(得分:1)

我认为这应该有效,但我无法进行测试。保持的一般语法

从表格 left join(statement)别名 在table.col = alias.col上 离开加入...

将您的陈述改为:

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
    ;  

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

答案 1 :(得分: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 
)        
;