以下任务的SQL语句是什么:

时间:2016-09-23 17:33:55

标签: mysql sql workbench

列出发布平装书的出版商以及每个出版的平装书数量。 我在计算每个出版商的平装本时遇到了困难。任何帮助都感激不尽。谢谢!

Henry Books Table Schema

**author**          
    authorNum       INT             PRIMARY KEY 
    authorLast      VARCHAR(12),    
    authorFirst     VARCHAR(10)

**publisher**           
    publisherCode       CHAR(3)         PRIMARY KEY 
    publisherName       VARCHAR(25)     
    city                VARCHAR(20)

**book**            
    bookCode            CHAR(4)         PRIMARY KEY 
    title               VARCHAR(40)     
    publisherCode       CHAR(3)     
    bookType            CHAR(3)     
    paperback           ENUM('No', 'Yes') 

    CONSTRAINT book_fk_publisher 
        FOREIGN KEY (publisherCode) 
        REFERENCES publisher(publisherCode) 

**branch**      
    branchNum       INT          
    branchName      VARCHAR(50)  
    branchLocation  VARCHAR(50) 

**copy** 
    bookCode        CHAR(4)  
    branchNum       INT  
    copyNum         INT     PRIMARY KEY 
    quality         ENUM('Excellent', 'Fair', 'Good', 'Poor') 
    price           DECIMAL(8,2) 

    CONSTRAINT copy_pk 
        PRIMARY KEY (bookCode, branchNum, copyNum 
    CONSTRAINT copy_fk_book 
        FOREIGN KEY (bookCode) 
        REFERENCES book(bookCode),      
    CONSTRAINT copy_fk_branch 
        FOREIGN KEY (branchNum) 
        REFERENCES branch(branchNum) 

**wrote** 
    bookCode   CHAR(4)  
    authorNum  INT  
    sequence   INT 

    PRIMARY KEY (BookCode, AuthorNum), 
    CONSTRAINT wrote_fk_book 
        FOREIGN KEY (bookCode) 
        REFERENCES book(bookCode), 
    CONSTRAINT wrote_fk_author 
        FOREIGN KEY (authorNum) 
        REFERENCES author(authorNum)    

这就是我所拥有的:

SELECT publisherName, COUNT(paperback) AS "numPaperback"
FROM publisher, book
WHERE paperback = "Yes";

2 个答案:

答案 0 :(得分:1)

好像你是新人。所以我会试着引导你完成这个......

  1. 将发布商表格加入图书表格。他们共享一个发布商代码,似乎将它们链接在一起。

  2. 使用where子句按书籍类型过滤联接的表格。你想要平装书。

  3. 对发布商代码和名称执行group by,然后count图书专栏

  4. 这有助于您入门吗?

答案 1 :(得分:1)

你必须使用 count(*) Group by 来计算平装书数。这就是我编写查询的方式:

SELECT publisherName,count(*) AS "Number of paperbacks" FROM
publisher p INNER JOIN book b WHERE
p.publisherCode=b.publisherCode AND b.paperback="Yes"
GROUP BY publisherName;