访问中的多个连接,请求参数值

时间:2015-10-05 11:58:04

标签: sql-server ms-access

在前一篇文章的帮助下,我能够为我想要加入的三个基本表的示例提供以下多表连接解决方​​案,但是我收到了PRIMARYKEY的参数问题。

select *
FROM       (((
SELECT [PRIMARYKEY] FROM [TABLE 1] 
UNION
SELECT [PRIMARYKEY] FROM [TABLE 2]
UNION
SELECT [PRIMARYKEY] FROM [TABLE 3] )                                                                AS           A
LEFT JOIN  [TABLE 1]      ON    A.[PRIMARYKEY]    =     [TABLE 1].[PRIMARYKEY]               )
LEFT JOIN [TABLE 2]      ON     A.[PRIMARYKEY]     =    [TABLE 2].[PRIMARYKEY]                )
LEFT JOIN [TABLE 3]      ON     A.[PRIMARYKEY]     =    [TABLE 3].[PRIMARYKEY]

我的想法是获得完整的主键列表,然后将其连接到其他表,因为我知道访问中没有完整的连接。

有没有人看到我的脚本有问题?它是“AS A”的一部分,它会把一切都扔掉吗?

1 个答案:

答案 0 :(得分:1)

请尝试使用别名的内容:

   SELECT *
     FROM ((( SELECT [PRIMARYKEY] AS MyField
                FROM [TABLE 1]
              UNION
              SELECT [PRIMARYKEY] AS MyField
                FROM [TABLE 2]
              UNION
              SELECT [PRIMARYKEY] AS MyField
                FROM [TABLE 3]
            ) AS A
            LEFT JOIN [TABLE 1]
                   ON A.[MyField] = [TABLE 1].[PRIMARYKEY] 
           )
           LEFT JOIN [TABLE 2]
                  ON A.[MyField] = [TABLE 2].[PRIMARYKEY] 
         )  
         LEFT JOIN [TABLE 3]
                ON A.[MyField] = [TABLE 3].[PRIMARYKEY]

你可以在访问中进行笛卡尔连接(这就是我知道完全连接的方式)。我经常使用一个名为dual的行表来总是返回一行......(就像在oracle中一样)。