2013-03-18 133 views
1

對於SQL評估,我必須執行一個查詢,我需要按銷售員代碼的順序列出過去一個月的汽車銷售情況。 我已經創建了一條SQL語句。但是,當我運行它時,除了查詢中包含的列名稱外,它什麼都沒有列出。我已經使用了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 
    
+3

歡迎來到Stack Overflow!請通過添加適當的標記(Oracle,SQL Server,MySQL等)來指定您要定位的RDBMS。可能會有利用不被普遍支持的語言或產品功能的答案。此外,通過使用特定的RDBMS標記它,您的問題可能會得到更適合回答的人的關注。 – Taryn 2013-03-18 16:00:31

回答

2

在你當前的查詢,你有一些不尋常的語法:

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

我不知道什麼是, Salesman應該做的。但是,如果你試圖JOINSalesman表,然後將查詢將是:

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

而且,使用的日期時,應通過磅SIGS #包圍。您的查詢應該是:

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; 
+0

感謝您的回覆。當我嘗試不包括推銷員運行SQL代碼,它提示我輸入訂單參數BY Salesman.Salesman代碼 – Alyn 2013-03-18 16:07:34

+0

@Alyn然後,你將需要'JOIN'到該表。查看我的編輯 – Taryn 2013-03-18 16:09:16

+0

當它顯示時,雖然它只是一個空白列表 – Alyn 2013-03-18 16:11:30