仅选择具有多个作者的图书,并显示图书标题、出版商和作者姓名

im9ewurl  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(592)

我需要制作以下文件:

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

需要添加/修改什么才能产生正确的输出?我感谢到目前为止我得到的支持,这是一个陡峭的学习曲线。

omjgkv6w

omjgkv6w1#

可以使用having 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

相关问题