查询语句得到结果汤姆和亚历克斯,谁写了比杂志更多的书。 如何编写sql语句来实现? 感谢。
答案 0 :(得分:0)
SQL:
SELECT ac.author, COUNT(b.*) AS number_of_books, COUNT(m.*) AS number_of_magazine
FROM AuthorCollection ac
LEFT JOIN book b ON b.key = ac.key
LEFT JOIN magazine m ON m.key = ac.key
GROUP BY ac.author;
在sql下面会给你一个确切的结果,
SELECT foo.author FROM (
SELECT ac.author, COUNT(b.*) AS number_of_books, COUNT(m.*) AS number_of_magazine
FROM AuthorCollection ac
LEFT JOIN book b ON b.key = ac.key
LEFT JOIN magazine m ON m.key = ac.key
GROUP BY ac.author
HAVING COUNT(b.*) > COUNT(m.*)
) foo;