如何编写sql语句比较

时间:2016-01-31 16:44:31

标签: postgresql

查询语句得到结果汤姆和亚历克斯,谁写了比杂志更多的书。 如何编写sql语句来实现? 感谢。

1 个答案:

答案 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;

Result

在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;