SQL Query涉及连接

时间:2013-03-18 15:59:13

标签: sql ms-access-2007

对于SQL评估,我必须执行一个查询,我需要列出过去一个月由Salesman Code订购的汽车销售。 我创建了一个SQL语句。但是,当我运行它时,除了我在查询中包含的列名之外,它根本没有列出任何内容。我使用的SQL代码如下:

SELECT CarForSale.SalesmanCode, 
  Salesman.SalesmanName, 
  Customer.CustomerCode, 
  Customer.CustomerName, 
  Customer.CustomerAddress
FROM 
(
  Centre 
  INNER JOIN Customer 
    ON Centre.CentreCode = Customer.CentreCode
) 
INNER JOIN CarForSale 
  ON (Customer.CustomerCode = CarForSale.CustomerCode) 
  AND (Centre.CentreCode = CarForSale.CentreCode), Salesman
WHERE CarForSale.CFSSoldDate BETWEEN CVDATE('01/02/2013') AND  CVDATE('01/03/2013')
ORDER BY Salesman.SalesmanCode;

如果有人有任何反馈或知道我哪里出错了,我会非常感谢这个问题的一些帮助。

以下是一些要求的示例数据:

  • CFS表

     CFSCode CFSMake CFSModel CFSSellingPrice 
     000001 Mercedes  W168         45000             
    
     CFSSoldDate CentreCode SalesmanCode CustomerCode
     01/03/2013   00000001   0000000001 00000001
    
  • 推销员表

    SalesmanCode SalesmanName SalemanCommThisYear SalesmanStatus CentreCode
    0000000001    Liam Jones       2250         senior        00000001
    
  • 客户表

    CustomerCode    CustomerName    CustomerAddress   CentreCode    CustomerStatus
    00000001    Gina Smith       1, The Lake,       00000002         good
                                 Merthyr Tydfil
    

1 个答案:

答案 0 :(得分:2)

在您当前的查询中,您有一些不寻常的语法:

AND (Centre.CentreCode = CarForSale.CentreCode), Salesman -- what's this

我不确定, Salesman应该做什么。但是,如果您在JOIN表上尝试Salesman,那么您的查询将是:

SELECT CarForSale.SalesmanCode, 
  Salesman.SalesmanName, 
  Customer.CustomerCode, 
  Customer.CustomerName, 
  Customer.CustomerAddress
FROM ((Centre 
INNER JOIN Customer 
  ON Centre.CentreCode = Customer.CentreCode) 
INNER JOIN CarForSale 
  ON (Customer.CustomerCode = CarForSale.CustomerCode) 
  AND (Centre.CentreCode = CarForSale.CentreCode)))
INNER JOIN Salesman
  ON CarForSale.SalesmanCode = Salesman.SalesmanCode
WHERE CarForSale.CFSSoldDate BETWEEN #01/02/2013# AND #01/03/2013#
ORDER BY Salesman.SalesmanCode;

同样在MS Access中,当使用日期时,它们应该被磅标记#包围。您的查询应该是:

SELECT CarForSale.SalesmanCode, 
  Salesman.SalesmanName, 
  Customer.CustomerCode, 
  Customer.CustomerName, 
  Customer.CustomerAddress
FROM ((Centre 
INNER JOIN Customer 
  ON Centre.CentreCode = Customer.CentreCode) 
INNER JOIN CarForSale 
  ON (Customer.CustomerCode = CarForSale.CustomerCode) 
  AND (Centre.CentreCode = CarForSale.CentreCode)))
INNER JOIN Salesman
  ON CarForSale.SalesmanCode = Salesman.SalesmanCode
WHERE CarForSale.CFSSoldDate BETWEEN #01/02/2013# AND #01/03/2013#
ORDER BY Salesman.SalesmanCode;

编辑#1,我不知道你为什么要加入Centre表,因为你没有使用它。我只是使用您的示例数据在MS Access中运行以下查询并获得结果:

SELECT CarForSale.SalesmanCode, 
  Salesman.SalesmanName, 
  Customer.CustomerCode, 
  Customer.CustomerName, 
  Customer.CustomerAddress
FROM (Customer 
INNER JOIN CarForSale 
  ON Customer.CustomerCode = CarForSale.CustomerCode)
INNER JOIN Salesman
  ON CarForSale.SalesmanCode = Salesman.SalesmanCode
WHERE CarForSale.CFSSoldDate BETWEEN #01/02/2013# AND #01/03/2013#
ORDER BY Salesman.SalesmanCode;