需要使用嵌入式查询进行多个连接的语法

时间:2013-11-27 07:45:11

标签: sql ms-access

M $ Access 2010

我有两张表CONTACT和AAEmail_X

这很棒

SELECT aa.[Contact ID], mm.Email AS [Personal Email]
FROM [CONTACT] AS aa
LEFT JOIN (
SELECT bb.[ClientID], bb.Email
FROM AAEmail_X AS bb
WHERE bb.Type = "Personal Email") AS mm
ON aa.[Contact ID] = mm.[ClientID];

注意括号中包含的嵌入式查询。

现在我想将其扩展为包含另一个嵌入式查询

SELECT aa.[Contact ID], mm.Email AS [Personal Email], nn.Email AS [Business Email]
FROM ( [CONTACT] AS aa
LEFT JOIN (
SELECT bb.[ClientID], bb.Email
FROM AAEmail_X AS bb
WHERE bb.Type = "Personal Email") AS mm
ON aa.[Contact ID] = mm.[ClientID]  )
LEFT JOIN (
SELECT cc.[ClientID], cc.Email
FROM AAEmail_X AS cc
WHERE cc.Type = "Business Email") AS nn
ON aa.[Contact ID] = nn.[ClientID];

请注意,由于Access'古怪的SQL实现

,第一次连接被包含在parantheses中

此操作失败,Access提示为“Query2.ClientID”

手动输入值

所以我觉得我纠缠在括号里。谁能发现什么是错的?

TIA,

仍在学习史蒂夫

3 个答案:

答案 0 :(得分:0)

试试这个,你在第一个FROM

之后开始了一个括号
SELECT aa.[Contact ID], mm.Email AS [Personal Email], nn.Email AS [Business Email]
FROM CONTACT AS aa
LEFT JOIN 
    (
        SELECT bb.[ClientID], bb.Email
        FROM AAEmail_X AS bb
        WHERE bb.Type = 'Personal Email'
    ) AS mm ON aa.[Contact ID] = mm.[ClientID]  

LEFT JOIN 
    (
        SELECT cc.[ClientID], cc.Email
        FROM AAEmail_X AS cc
        WHERE cc.Type = 'Business Email'
    ) AS nn ON aa.[Contact ID] = nn.[ClientID];

答案 1 :(得分:0)

我认为你的括号是问题...... 尝试

SELECT aa.[Contact ID], mm.Email AS [Personal Email], nn.Email AS [Business Email]
    FROM ( [CONTACT] AS aa
    LEFT JOIN (
        SELECT bb.[ClientID], bb.Email
        FROM AAEmail_X AS bb
        WHERE bb.Type = "Personal Email") AS mm
    ON aa.[Contact ID] = mm.[ClientID]
    LEFT JOIN (
        SELECT cc.[ClientID], cc.Email
        FROM AAEmail_X AS cc
        WHERE cc.Type = "Business Email") AS nn
    ON aa.[Contact ID] = nn.[ClientID]
);

SELECT aa.[Contact ID], mm.Email AS [Personal Email], nn.Email AS [Business Email]
    FROM (( [CONTACT] AS aa
    LEFT JOIN (
        SELECT bb.[ClientID], bb.Email
        FROM AAEmail_X AS bb
        WHERE bb.Type = "Personal Email") AS mm
    ON aa.[Contact ID] = mm.[ClientID])
    LEFT JOIN (
        SELECT cc.[ClientID], cc.Email
        FROM AAEmail_X AS cc
        WHERE cc.Type = "Business Email") AS nn
    ON aa.[Contact ID] = nn.[ClientID]
);

答案 2 :(得分:0)

这很有用。必须有一些微妙的拼写错误或语法错误。

SELECT aa.[Contact ID], mm.Email AS [Personal Email], nn.Email AS [Business Email]
FROM ( [CONTACT] AS aa
LEFT JOIN (SELECT bb.[ClientID], bb.Email FROM AAEmail_X AS bb WHERE bb.Type = "Personal     Email")  AS mm
ON aa.[Contact ID] = mm.[ClientID] )
LEFT JOIN (SELECT cc.[ClientID], cc.Email FROM AAEmail_X AS cc WHERE cc.Type = "Business Email")  AS nn
ON aa.[Contact ID] = nn.[ClientID];

感谢所有回答的人! Stackoverflow晃动!

再次感谢,

CASE CLOSED

仍在学习史蒂夫