SQL select 语句针对 table 的多个特定区域
SQL select statement targeting multiple specific areas of a table
我能做到这一点,但我想不通的是如何在 count() 结果之前列出分支的名称。
我还需要一种方法来同时显示所有分支机构和该特定分支机构的借书数量。如果有人能帮我解决这个问题,那就太好了,谢谢!
结果看起来像
"Branch Name".."Number of loaned books"
"Next Branch".."Number of loaned books"
代码:
select count(bookLoans.bookID) from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
where libraryBranches.branchName = 'Sharpstown'
按赞分组即可:
select count(bookLoans.bookID), libraryBranches.branchName from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
Group by libraryBranches.branchName
我不确定你想做什么,但我会尽力回答。
对于你的第一个问题,你只需要在你的 "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;
我能做到这一点,但我想不通的是如何在 count() 结果之前列出分支的名称。
我还需要一种方法来同时显示所有分支机构和该特定分支机构的借书数量。如果有人能帮我解决这个问题,那就太好了,谢谢!
结果看起来像 "Branch Name".."Number of loaned books" "Next Branch".."Number of loaned books"
代码:
select count(bookLoans.bookID) from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
where libraryBranches.branchName = 'Sharpstown'
按赞分组即可:
select count(bookLoans.bookID), libraryBranches.branchName from bookLoans
inner join libraryBranches on bookLoans.lbID = libraryBranches.lbID
Group by libraryBranches.branchName
我不确定你想做什么,但我会尽力回答。
对于你的第一个问题,你只需要在你的 "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;