有两个表: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
答案 0 :(得分:0)
使用左联接和现代联接而不用逗号分隔联接
Select authors.name, count(books.authorid)
FROM authors left join books
on authors.authorid = books.authorid
group by authors.name