寻找作者人数

时间:2019-03-27 15:37:05

标签: sql

有两个表:authors(authorid, name)books(bookid, authorid, title, year)

我可以编写哪种sql查询来计算books表中有多少作者,如果某个作者没有本书,则返回零?

我尝试过:

Select authors.name, count(authors.authorid) as count
FROM authors, books
where authors.authorid = books.authorid
group by authors.authorid;

示例输出:

name count
author1 1
author2 0

1 个答案:

答案 0 :(得分:0)

使用左联接和现代联接而不用逗号分隔联接

Select authors.name, count(books.authorid)
FROM authors left join books
on authors.authorid = books.authorid
 group by authors.name