我有两张桌子:作者和书籍。
作者中的我有属性authorID,authorName和authorDOB authorID是此表中的主键。
书表中的我有属性bookISBN,authorID等。
bookISBN作为主键,authorID作为外键
我正在尝试执行一个给定作者姓名的查询,执行一个
该作者所有书籍的数量。
这是我得到的:
SET @ID =
AuthorID
FROM authors
WHERE ('Mark Twain' = AuthorName);
SELECT COUNT(*)
FROM books
WHERE (AuthorID = ID);
非常感谢任何帮助
答案 0 :(得分:0)
尝试:
SELECT a.authorId, a.authorName, count(*)
FROM authors a
INNER JOIN books b ON b.AuthorID=a.AuthorID
WHERE ('Mark Twain' = a.AuthorName)
GROUP BY a.authorId, a.authorName
答案 1 :(得分:0)
我正在尝试执行查询,其中给定作者姓名,执行该作者对所有图书的计数。
尝试
select count(1)
from books b
inner join authors a on a.AuthorID=b.AuthorID
where a.AuthorName='Mark Twain'
答案 2 :(得分:0)
如果您认为自己会更频繁地进行搜索,也可以使用某个功能。只是一个想法。
go
create function totalbooksbyauthor (@authorname varchar(20) ) returns table
as return
select a.authorid, a.authorname, COUNT(b.bookname) bookcount
from authors a
inner join books b
on a.authorID = b.authorID
where a.authorname = @authorname
group by a.authorid, a.authorname
go
select * from totalbooksbyauthor ('Mark Twain')