2016-12-01 6 views
0

は、クエリがあると私はエラーを取得:MYSQLのエラー

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, 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 
    ) 
); 

私はあなたがどのInを避けることができると思う:

+1

ヒント:1つのステートメントにSQLコードを記述する必要はありません。キャリッジリターンとインデントは、他の種類のコードと同様に使用できます。私の編集例を見てください。 –

答えて

0

は、あなたが持っのみ、選択した列であるので、あなたがエラーを取得し

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 
       ) 
      ); 
0

により、中間グループを必要とするかもしれません

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