没有库存时显示零

时间:2015-12-31 22:40:56

标签: ms-access-2013

我有一个简单的查询,显示由特定作者撰写的书籍,并计算库存中的书籍总数。查询返回的结果显示五列。 BookID,AuthorID,AuthorName,BookName以及库存中的总账簿。

我的问题是,如果某本书没有库存,则书名不会显示在结果中。我希望显示的书名显示为零。

1 1 Lee Child Personal 5

1 2 Lee Child Make Me 4

1 3 Lee Child The Visitor 0

1 p 4 Lee Child One Shot 8

查询的sql如下所示:

SELECT tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,  
tblBook.BookName, Count(tblBook.BookName) AS [In Stock]
FROM (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) 
INNER JOIN tblSale ON tblBook.BookID = tblSale.BookID
GROUP BY tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,   
tblBook.BookName, tblSale.BookInStock
HAVING (((tblAuthor.AuthorID)=[forms]![frmMain]![text12]) AND  
((tblSale.BookInStock)=Yes))
ORDER BY tblAuthor.AuthorName;

我非常感谢解决这个恼人的问题!非常感谢......

1 个答案:

答案 0 :(得分:0)

不得不说没有更多信息,但你的查询有 (tblSale.BookInStock)=是)

不是过滤掉所有的没有

像这样的东西(Just the Zero Stock Books)?

SELECT tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,  
tblBook.BookName, Count(tblBook.BookName) AS [In Stock]
FROM (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) 
INNER JOIN tblSale ON tblBook.BookID = tblSale.BookID
Where [In Stock] = 0    
GROUP BY tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,   
tblBook.BookName, tblSale.BookInStock
ORDER BY tblAuthor.AuthorName;

或(包括零库存的所有书籍)

SELECT tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,  
tblBook.BookName, Count(tblBook.BookName) AS [In Stock]
FROM (tblAuthor INNER JOIN tblBook ON tblAuthor.AuthorID = tblBook.AuthorID) 
INNER JOIN tblSale ON tblBook.BookID = tblSale.BookID
GROUP BY tblSale.BookID, tblAuthor.AuthorID, tblAuthor.AuthorName,   
tblBook.BookName, tblSale.BookInStock
ORDER BY tblAuthor.AuthorName;