查询如下,我收到错误:
SELECT a.name
FROM author a, catalog c
WHERE a.authorid = c.authorid
AND c.bookid IN (
SELECT bookid
FROM orderdetails
GROUP BY bookid
HAVING sum(quantity) = (
SELECT max(sum(quantity))
FROM orderdetails
)
);
以下表由书经销商维护。
AUTHOR (author-id:int, name:string, city:string, country:string)
PUBLISHER (publisher-id:int, name:string, city:string, country:string)
CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int, price:int)
CATEGORY (category-id:int, description:string)
ORDER-DETAILS (order-no:int, book-id:int, quantity:int)
和问题是:
iv。查找本书的作者最大销售。
可能是您需要一个中间组
select a.name
from author a
inner join catalog c on a.authorid=c.authorid
and c.bookid in (select bookid
from orderdetails
group by bookid
having sum(quantity) = (
select max( sum_quantity) from (
select sum(quantity) sum_quantity
from orderdetails
group by bookid
)
);
您会收到错误,因为只有在您选择的列上,
SELECT a.name
FROM author a, catalog c
WHERE a.authorid = c.authorid
AND c.bookid IN (
SELECT bookid, sum(quantity) as total_qty
FROM orderdetails
GROUP BY bookid
HAVING total_qty = (
SELECT max(sum(quantity))
FROM orderdetails
)
);
我认为您可以避免尝试获得较少的查询最好的问题:
SELECT a.name
FROM author a
INNER JOIN catalog c ON a.authorid = c.authorid
INNER JOIN (
SELECT bookid, sum(quantity) as total_qty
FROM orderdetails
GROUP BY bookid
HAVING total_qty = (
SELECT max(sum(quantity))
FROM orderdetails
)
) t ON t.bookid = c.bookid