仅选择具有多位作者的书并显示书名,出版商和作者名

时间:2018-12-11 19:16:59

标签: mysql

我需要提供以下内容:

Title         Publisher Name   Author Last  Author First
========================================================
Treasure      Tauntan Press     Smith       John
Treasure      Tauntan Press     Jones       Andrew 

我的代码如下:

SELECT 
Book_Title as 'Title',
Publisher_Name,
Author_Last,
Author_First
FROM BOOK
JOIN PUBLISHER USING (Publisher_Code)
JOIN WROTE USING (Book_Code)
JOIN AUTHOR USING (Author_Num);

select BOOK.* 
FROM BOOK
JOIN PUBLISHER USING (Publisher_Code)
JOIN WROTE USING (Book_Code)
JOIN AUTHOR USING (Author_Num)
inner join (
    SELECT 
    Book_Title as Title,
    Publisher_Name
    FROM BOOK
    JOIN PUBLISHER USING (Publisher_Code)
    JOIN WROTE USING (Book_Code)
    JOIN AUTHOR USING (Author_Num)
    group by  Book_Title, Publisher_Name
    having count(distinct concat(Author_Last,Author_First)) = 2
) t on t.Title = BOOK.Book_Title 
        and t.Publisher_Name = PUBLISHER.Publisher_Name

它会产生以下结果:

Code      Book Title  Book Type   Book Paperback  Publisher Code
=================================================================
 1234    Treasure      Art       FALSE            AAA
 1234    Treasure      Art       FALSE            AAA

需要添加/修改哪些内容才能产生正确的输出?感谢您到目前为止获得的支持,这是一个艰难的学习过程。

1 个答案:

答案 0 :(得分:0)

您可以使用让count(distinct concat(LastName,FirstName))= 2 按标题和发布者分组

    SELECT 
    Book_Title as 'Title',
    Publisher_Name
    FROM BOOK
    JOIN PUBLISHER USING (Publisher_Code)
    JOIN WROTE USING (Book_Code)
    JOIN AUTHOR USING (Author_Num)
    group by  Boo, k_Title, Publisher_Name
    having count(distinct concat(LastName,FirstName)) = 2

并获取与这些书名相关的书中的所有值

select BOOK.* 
FROM BOOK
JOIN PUBLISHER USING (Publisher_Code)
JOIN WROTE USING (Book_Code)
JOIN AUTHOR USING (Author_Num)
inner join (
    SELECT 
    Book_Title as Title,
    Publisher_Name
    FROM BOOK
    JOIN PUBLISHER USING (Publisher_Code)
    JOIN WROTE USING (Book_Code)
    JOIN AUTHOR USING (Author_Num)
    group by  Boo, k_Title, Publisher_Name
    having count(distinct concat(LastName,FirstName)) = 2
) t on t.Title = BOOK.Book_Title 
        and t.Publisher_Name = PUBLISHER.Publisher_Name