针对表

时间:2017-09-27 21:01:41

标签: sql sql-server-2014-express

我能够做到这一点,但我无法想象的是如何在count()结果之前列出分支的名称。

我还需要一种方法来同时向所有特定分支显示所有分支和借出的书籍数量。如果有人能帮助我,这将是惊人的,谢谢!

结果看起来像 "分行名称" .."借书数量" " Next Branch" .."借书数量"

代码:

select count(bookLoans.bookID) from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
where libraryBranches.branchName = 'Sharpstown'

2 个答案:

答案 0 :(得分:0)

只需使用Group by:

select count(bookLoans.bookID), libraryBranches.branchName from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
Group by libraryBranches.branchName

答案 1 :(得分:0)

我不确定你要做什么,但我会尽力回答。

对于您的第一个问题,您只需要在“select”中添加branchName,并在请求的末尾添加“group by”:

select libraryBranches.branchName, count(bookLoans.bookID) from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
where libraryBranches.branchName = 'Sharpstown'
group by libraryBranches.branchName;

要获取结果中的所有分支,只需删除“where”部分。如果需要,您可以在最后添加“order by”:

select libraryBranches.branchName, count(bookLoans.bookID) from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
group by libraryBranches.branchName
order by libraryBranches.branchName;