错误“指定的字段可以引用FROM子句中的多个表”

时间:2013-01-02 20:42:35

标签: sql alias ms-access-2010

我正在使用Access 2010。

我的错误似乎是别名的结果。

我读过的解决方案指出应该在每个级别(我已经完成)使用不同的别名,并且Access喜欢大量的SELECT *(所以我也添加了这些)但是它没有解决我的问题问题。

我在ServiceZipSpec.Service_Product上收到错误。有什么指针吗?谢谢!

SELECT DISTINCT ServiceZipSpec.Service_Product, ServiceZipSpec.Specificity, Service_Availability.Available, Service_Availability.Zip_Code, Service_Availability.State
    FROM 
    ( SELECT * FROM 
       ( SELECT * FROM Service_Availability AS C_Avail1
         INNER JOIN 
            (
              SELECT C_Avail2.Service_Product, MAX(C_Avail2.Specificity) AS Spec
              FROM Service_Availability AS C_Avail2
              WHERE (C_Avail2.State = "TX" OR C_Avail2.State = "CW")
              GROUP BY C_Avail2.Service_Product 
            ) MaxSpec
         ON C_Avail1.Service_Product = MaxSpec.Service_Product
             AND C_Avail1.Specificity = MaxSpec.Spec
       ) Service_Spec
    LEFT JOIN TABLE_ZipCodes ON Service_Spec.State = TABLE_ZipCodes.State
    ) ServiceZipSpec

    WHERE ServiceZipSpec.Available = TRUE AND (ServiceZipSpec.State = "TX" OR ServiceZipSpec.State = "CW")
    ;

3 个答案:

答案 0 :(得分:5)

问题的根源在于您在连接具有类似命名列的表时使用SELECT *。尝试限制为SELECT table_name.*,或者只选择您需要的列。

例如:

SELECT DISTINCT ServiceZipSpec.Service_Product, ServiceZipSpec.Specificity, Service_Availability.Available, Service_Availability.Zip_Code, Service_Availability.State
  FROM 
    ( SELECT Service_Spec.*, TABLE_ZipCodes.Zip_Code FROM 
       ( SELECT C_Avail1.* FROM Service_Availability AS C_Avail1
...

答案 1 :(得分:2)

您需要SELECT colName1, colname2而不是SELECT *,您的子查询中会出现冲突。您的外部选择列表也将来自包含别名ServiceZipSpec的表格:

SELECT DISTINCT ServiceZipSpec.Service_Product, 
    ServiceZipSpec.Specificity, 
    ServiceZipSpec.Available, 
    ServiceZipSpec.Zip_Code, 
    ServiceZipSpec.State
FROM 
( 
    SELECT colName1, colname2, etc -- name your columns here
    FROM 
    ( 
        SELECT colName1, colname2, etc -- name your columns here
        FROM Service_Availability AS C_Avail1
        INNER JOIN 
        (
          SELECT C_Avail2.Service_Product, MAX(C_Avail2.Specificity) AS Spec
          FROM Service_Availability AS C_Avail2
          WHERE (C_Avail2.State = "TX" OR C_Avail2.State = "CW")
          GROUP BY C_Avail2.Service_Product 
        ) MaxSpec
        ON C_Avail1.Service_Product = MaxSpec.Service_Product
            AND C_Avail1.Specificity = MaxSpec.Spec
   ) Service_Spec
    LEFT JOIN TABLE_ZipCodes 
        ON Service_Spec.State = TABLE_ZipCodes.State
) ServiceZipSpec
WHERE ServiceZipSpec.Available = TRUE 
    AND (ServiceZipSpec.State = "TX" OR ServiceZipSpec.State = "CW");

答案 2 :(得分:1)

指定的字段引用可以引用SQL语句的FROM子句中列出的多个表。在以下示例中,“订单”和“订单明细”表中都存在“订单ID”字段:

SELECT OrderID 
FROM Orders, [Order Details];

由于该语句未指定OrderID属于哪个表,因此会产生此错误。要完成此操作,请通过添加表名完全限定字段引用。例如:

SELECT Orders.OrderID 
FROM Orders, [Order Details];