我正处于最后一个“高级选择陈述”问题,似乎无法得到正确答案。
表1:书虫数据库的模式。主键带下划线。有一些外键引用将表连接在一起;你可以利用天然连接来利用它们。
Author(aid, alastname, afirstname, acountry, aborn, adied).
Book(bid, btitle, pid, bdate, bpages, bprice).
City(cid, cname, cstate, ccountry).
Publisher(pid, pname).
Author_Book(aid, bid).
Publisher_City(pid, cid).
问题是......“查找已经写过三本或更多书籍的作者的姓名。”
代码有效但我想要完整的作者姓名而不是作者ID ..
select count(aid) as authorBook, aid as authorName
from Author_book natural join Author
group by aid
having count(aid) > 3;
决赛桌出现了......
authorbook | authorname
------------+------------
8 | dick
4 | thar
(2 rows)
答案 0 :(得分:2)
您需要HAVING COUNT(aid) > 3
您不能使用别名authorBook。
如果您需要作者姓名及其在表格中的名称,您可以这样做:
SELECT AuthorName, COUNT(*) AS CountOfBooks
FROM AuthorBooks GROUP BY AuthorName HAVING COUNT(*) > 3
如果作者的姓名是外键,请执行以下操作:
SELECT x.AuthorName, COUNT(x2.*) FROM
AuthorBooks x2 INNER JOIN Authors x1 ON x1.AuthorID = x2.AuthorID
GROUP BY x.AuthorName HAVING COUNT(x2.*) > 3
答案 1 :(得分:0)
SELECT A.alastname,
A.afirstname,
COUNT(*) as Books
FROM Author_Book AB
INNER JOIN Author A
ON AB.aid = A.aid
GROUP BY aid
HAVING COUNT(*) > 3