2014-04-23 188 views
0

在下面的查詢中,我試圖做一個快速彙總表來顯示一些信息。使用where子句是爲了從數據庫中找到與該發佈者相關的信息。在查看其他定義一個小時左右後,我看不清楚我的錯在哪裏。SQL where子句什麼都不返回

對不起,這可能是一件很簡單的事情,但是下面的查詢有什麼明顯的錯誤。

SELECT Publisher.Name AS "Publisher Name", 
     Book.Title AS "Book Title", 
     ShopOrder.OrderDate AS "Order Date", 
     OrderLine.Quantity AS "Order Quantity", 
     (SUM(Quantity*UnitSellingPrice)) AS "Total Value" 
    FROM ShopOrder 
    INNER JOIN OrderLine 
    ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID 
    INNER JOIN Book 
    ON OrderLine.BookID=Book.BookID 
    INNER JOIN Publisher 
    ON Publisher.PublisherID=Book.PublisherID 
    WHERE Publisher.Name='Oxford' 
    GROUP BY ShopOrder.ShopOrderID, 
      Publisher.PublisherID, 
      Book.BookID, 
      OrderLine.Quantity 
    ORDER BY ShopOrder.OrderDate; 

非常感謝任何人的幫助!

編輯: 相關表DEFS

CREATE TABLE Publisher 
(
    PublisherID INTEGER, 
    Name VARCHAR(50), 

    PRIMARY KEY(PublisherID) 
); 

CREATE TABLE Book 
(
    BookID INTEGER, 
    Title VARCHAR(50), 
    Price DECIMAL (10, 2), 
    CategoryID INTEGER REFERENCES Category(CategoryID), 
    PublisherID INTEGER REFERENCES Publisher(PublisherID), 

    PRIMARY KEY(BookID) 

); 

CREATE TABLE ShopOrder 
(
    ShopOrderID INTEGER, 
    OrderDate DATE, 
    ShopID INTEGER REFERENCES Shop(ShopID), 
    SalesRepID INTEGER REFERENCES SalesRep(SalesRepID), 

    PRIMARY KEY(ShopOrderID) 
); 

CREATE TABLE OrderLine 
(
    ShopOrderID INTEGER REFERENCES ShopOrder(ShopOrderID) ON DELETE CASCADE, 
    BookID INTEGER REFERENCES Book(BookID), 
    Quantity INTEGER, 
    UnitSellingPrice DECIMAL (10,2) 
); 
+2

做的所有連接表都有記錄的數據,其中'Publisher.Name ='Oxford''?從「Publisher」開始,並加入其他表以查看斷開連接的位置。 –

+0

@ d-stanley我是一個SQL noob,所以我不完全確定你的意思。我真的很討厭問這麼愚蠢的問題,因爲我可能是愚蠢的,只是在竊聽你。你能否更詳細地解釋。如果不是不用擔心:) – RyanH

+1

只是爲了測試上述評論,將'INNER JOIN Publisher'改爲'RIGHT JOIN Publisher' –

回答

1

這不是一個明確的答案,但我已經添加它來展示如何將調試查詢:

開始Publisher

SELECT Publisher.Name AS "Publisher Name" 
FROM Publisher 
WHERE Publisher.Name='Oxford' 

查看結果...

添加Book

SELECT Publisher.Name AS "Publisher Name", 
    Book.Title AS "Book Title" 
FROM Book 
INNER JOIN Publisher 
ON Publisher.PublisherID=Book.PublisherID 
WHERE Publisher.Name='Oxford' 

檢查結果...

添加OrderLine

SELECT Publisher.Name AS "Publisher Name", 
    Book.Title AS "Book Title", 
    OrderLine.Quantity AS "Order Quantity" 
FROM OrderLine 
INNER JOIN Book 
    ON OrderLine.BookID=Book.BookID 
INNER JOIN Publisher 
    ON Publisher.PublisherID=Book.PublisherID 
WHERE Publisher.Name='Oxford' 

檢查結果...

添加ShopOrder

SELECT Publisher.Name AS "Publisher Name", 
    Book.Title AS "Book Title", 
    ShopOrder.OrderDate AS "Order Date", 
    OrderLine.Quantity AS "Order Quantity" 
FROM ShopOrder 
INNER JOIN OrderLine 
    ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID 
INNER JOIN Book 
    ON OrderLine.BookID=Book.BookID 
INNER JOIN Publisher 
    ON Publisher.PublisherID=Book.PublisherID 
WHERE Publisher.Name='Oxford' 

檢查結果...

添加SUM和分組:

SELECT Publisher.Name AS "Publisher Name", 
    Book.Title AS "Book Title", 
    ShopOrder.OrderDate AS "Order Date", 
    OrderLine.Quantity AS "Order Quantity", 
    (SUM(Quantity*UnitSellingPrice)) AS "Total Value" 
FROM ShopOrder 
INNER JOIN OrderLine 
    ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID 
INNER JOIN Book 
    ON OrderLine.BookID=Book.BookID 
INNER JOIN Publisher 
    ON Publisher.PublisherID=Book.PublisherID 
WHERE Publisher.Name='Oxford' 
GROUP BY ShopOrder.ShopOrderID, 
     Publisher.PublisherID, 
     Book.BookID, 
     OrderLine.Quantity 
ORDER BY ShopOrder.OrderDate; 
+0

非常感謝@ d-stanley。通過它反向工作,我現在看到了問題。我的GROUP BY中的舊信息似乎是導致問題的原因。同時也感謝,因爲我現在有很好的方法來調試這個在未來的信息! – RyanH

0

斯坦利的答案是這種情況,但任何人真正幫莫名其妙的未來尋找這個問題的答案正確的查詢應該如下。

SELECT Publisher.Name AS "Publisher Name", Book.Title AS "Book Title", ShopOrder.OrderDate AS "Order Date", 
OrderLine.Quantity AS "Order Quantity", (SUM(Quantity*UnitSellingPrice)) AS "Total Value" 
FROM ShopOrder 
INNER JOIN OrderLine 
ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID 
INNER JOIN Book 
ON OrderLine.BookID=Book.BookID 
INNER JOIN Publisher 
ON Publisher.PublisherID=Book.BookID 
WHERE Publisher.Name='Oxford' 
GROUP BY publisher.name, book.title, shoporder.orderdate, orderline.quantity 
ORDER BY ShopOrder.OrderDate;