我有一些任务可以帮助我练习SQL,但遇到了一些麻烦
我的数据库中有2个表-> authors(作者ID,名称)和books(bookId,authorId,标题)。
所以我的任务的第一部分是将作者及其书籍数量聚集在一起,所以我做到了:
SELECT authors.name, COUNT(*) AS 'number of books' FROM authors, books WHERE authors.authorId=books.authorId GROUP BY authors.name;
现在我需要向作者显示他们的书本,包括零本书的作者,我该怎么做?
谢谢!
答案 0 :(得分:0)
您必须使用SELECT authors.name, sum(case when bookId is null then 0 else 1 end) AS 'number of books' FROM authors
left join books on authors.authorId=books.authorId
GROUP BY authors.name;
import Foundation
let now = Date()
let calendar = Calendar.current
let date = calendar.date(bySettingHour: 0,
minute: 0,
second: 0,
of: now,
direction: .backward)