指定的字段<field>可以引用SQL语句的FROM子句中列出的多个表

时间:2017-12-28 22:11:31

标签: sql ms-access

我有一个问题:我在Access中有3个表 -

Assignments (Date, Parent Company ID, Sales Rep ID)
Roster (Date, Sales Rep ID, Team ID)
Goals (Date, Team ID)

首先,我离开了加入Roster的作业以获得最终的Roster表格,我试图在Goals上加入这个新表格,但我收到的错误是:< / p>

Specified field FR.date could refer to more than one table listed in the from clause of your SQL statement. 

下面是SQL:

SELECT * 
  from Goals as G
         LEFT JOIN
            (SELECT *
               FROM Assignments AS A 
                     LEFT JOIN Roster AS R 
                       ON (A.[Sales Rep ID] = R.[User ID]) 
                        AND (A.Date = R.Date)
            ) FR 
         on (G.Date = FR.Date)
             AND G.[Team ID] = FR.[Team ID]

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

此查询

SELECT *
           FROM Assignments AS A 
                 LEFT JOIN Roster AS R 
                   ON (A.[Sales Rep ID] = R.[User ID]) 
                    AND (A.Date = R.Date)

包含2个具有相同字段名称的列,在本例中为日期,当您编写G.Date = FR.Date时,您对字段日期的含糊不清的引用表A或R

答案 1 :(得分:0)

虽然MS Access在FROM子句中需要额外的括号,但您不需要这样的子查询:

SELECT * 
FROM (Goals as G LEFT JOIN
      Assignments AS A 
      ON G.Date = A.Date AND G.[Team ID] = A.[Team ID] 
     ) LEFT JOIN
     Roster as R
     ON A.[Sales Rep ID] = R.[User ID] AND A.Date = R.Date;

目前还不清楚[Team ID]来自何处,因此您可能需要将该条件放在第二个ON子句中。