简体   繁体   中英

MYSQL nested inner join query

I wanted to create a nested query that on the outside gets the title and price from a table called 'Books' while having a nest inside the query that gets the Author's First and last name for that specific book. I'm just a little confused on the Inner Joins and where they need to be placed. This is as close as I was able to get with it but this just prints every author for every book.

select Title, AuthorFirst, AuthorLast,Price from Book
JOIN
(select AuthorLast,AuthorFirst from Author 
INNER JOIN Wrote on Author.AuthorNum = Wrote.AuthorNum 
INNER JOIN Book on Wrote.BookCode = Book.BookCode group by title desc)Auth;

This joins the tables that I need but it prints every author in the DB with every book in the database. I think it has something with my Inner Joins not being specific enough.

The group by clause is wrong and you should remove it. Once you do that, there's no need to nest the join s - you could just have several joins in the same query:

SELECT     Title, AuthorFirst, AuthorLast, Price
FROM       Book
INNER JOIN Wrote ON Author.AuthorNum = Wrote.AuthorNum 
INNER JOIN Book ON Wrote.BookCode = Book.BookCode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM