MYSQL nested inner join query - mysql

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 joins - 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

Related

How to include a full column using a join

I'm doing an homework assignment and I have most of it but I can't get the last part. I don't know how to "Include artists without albums in your listing"?
SELECT
artist.name, Title
FROM
artist
INNER JOIN
album ON artist.artistID = album.artistid
ORDER BY
name, title;
This is what I get (top) and what I'm supposed to get (bottom):
You need to use LEFT JOIN instead of INNER JOIN in this case.
Different joins explanation with examples could be found in many places, for example in wikipedia

Joining one table all columns and second table few columns

I have two tables:- PERSON and DATA. What I want to do is fetch all details from PERSON table and only two columns from DATA table only when PERSON.personId = DATA.personId.
I am using this query:-
SELECT *
FROM PERSON AND SELECT DATA.value, DATA.field
FROM DATA where PERSON.personId = DATA.personId;
But I think this is wrong syntax. Can anyone tell me what is the right syntax for it.
SELECT p.*,d.column1,d.column2
FROM Person p
JOIN Data d
ON p.personId = d.personId
WHERE <Condition>
In this query person with all columns and data with your desire column you can fetch by this query.
Something like this:
select
P.*,
D.value,
D.field
from Person P
join Data D on P.PersonID = D.PersonID
change P.* to the specific columns that you need but P.* will get everything from the Person table.
check this post out LEFT JOIN vs. LEFT OUTER JOIN in SQL Server to learn about JOINS, the diagram is good to understand what the different ones do
Its really easy, Just execute this query:
SELECT
PERSON.*,
DATA.value,
DATA.field
FROM
PERSON INNER JOIN DATA USING (`personId`);
It selects all fields of PERSON + value and field from DATA.
Also it uses personId to join the two tables.
Fill free to ask if you need more info.
You can use join (LEFT JOIN)
SELECT * FROM PERSON LEFT JOIN DATA ON PERSON.personId = DATA.personId
Hope it will help you
Here's the correct syntax for achieving what you've asked for:
SELECT PERSON.column1,PERSON.column2,PERSON.columnN,DATA.value
FROM PERSON
INNER JOIN DATA
ON PERSON.personId = DATA.personId
Line#1: lists the columns that you want to select with references to their parent tables.
Line#2 and 3: are the two tables that you want to select from and join with
Line#4: is the join condition between the two tables (with matching IDs or other information)

How can i get book's title and author's firstname into the same result table when my database is designed like this

I have 3 tables
1. titles table have 2 fields(title,isbn)
2. authorisbn table have 2 fields(authorid,isbn)
3. authors table have 2 fields(authorid,firstname)
so i want to get a book's title(where the title is like '%Java%') and author's firstname of that book to fill in the same result table
what is the sql statement should i have to use to achieve that
my sql statement is like this
select titles.title, titles.isbn, authorisbn.authorid
from titles inner join authorisbn
where title like '%Java%' and authorisbn.isbn = titles.isbn
but it does not work because i can get only authorid
sorry for my english im learning it as well
I think this is your answer:
select v1.title, v1.isbn,authorisbn.authorid, authors.firstname
FROM books v1
INNER join authorisbn
ON v1.isbn = authorisbn.isbn
INNER JOIN books v2
on v2.isbn = authorisbn.isbn
INNER JOIN authors
WHERE v2.title like '%Java%';
Here, v1 and v2 are representing the books table, but when we use it multiple times in our query we should define them in new variables.
If this helps, please check it as an answer
try this.
select titles.title, titles.isbn, authorisbn.authorid, authors.firstname
from authorisbn
left join titles on titles.isbn = authorisbn.isbn
left join authors on authors.authorid = authorisbn.authorid
where titles.title like '%Java%'
you need to get to the authors table to get authors.firstname
select titles.title, titles.isbn, authorisbn.authorid, authors.firstname
from titles
inner join authorisbn
on authorisbn.isbn = titles.isbn
and title like '%Java%'
inner join authors
on authors.authorid = authorisbn .authorid
can move title like '%Java%' down into a where clause
where title like '%Java%'

MySQL: how to get result from 2 tables without repeating results?

I've got 3 tables: book, publisher, book_category
For a particular book category (fantasy) I have to display list of publisher names supplying that genre.
publisher_name and category_name are linked through book table, so my query is:
SELECT publisher.publisher_name
FROM publisher, book, book_category
WHERE publisher.publisher_id = book.publisher_id
AND book.category_id = book_category.category_id
AND category_name = 'fantasy';
But the result I'm getting is repeating the name of publisher if there's more than one fantasy book supplied by that publisher.
Let's say I've got The Hobbit and The Lord of the Rings,both are fantasy and are supplied by the same PublisherA.
In that case the result of my query is:
PublisherA
PublisherA
Is it possible to get that result just once? Even if there's much more than 2 fantasy books
published by the same publisher?
Just use distinct if you only need publisher_name
SELECT distinct publisher.publisher_name
by the way, try to use JOIN syntax... to join tables
SELECT distinct p.publisher_name
FROM publisher p
join book b on b.publisher_id = p.publisher_id
join book_Category bc on bc.category_id = b.category_id
where bc.category_name = 'fantasy'
Use DISTINCT
SELECT DISTINCT publisher.publisher_name
FROM publisher, book, book_category
WHERE publisher.publisher_id = book.publisher_id
AND book.category_id = book_category.category_id
AND category_name = 'fantasy';
Try adding this to the end of the query: GROUP BY publisher.publisher_name
Everyone is mentioning DISTINCT, which is correct (better than GROUP BY in MySQL, because of the way the optimizer is set up), but I figured I would also add a modification for performance enhancements.
Currently you have implicit cross joins to get to the other tables, and making these explicit INNER JOINs will increase efficiency because of the order of filtering. Example:
SELECT DISTINCT Publisher.publisher_name
FROM publisher Publisher
INNER JOIN book Book ON Publisher.publisher_id = Book.publisher_id
INNER JOIN book_category Book_Category ON Book.category_id = Book_Category.category_id
WHERE Book_Category.category_name = 'fantasy';
In the original query, you bring in the complete record set of all three tables (publisher, book, book_category), and then from that set you join on the respective keys, and then return the result set. In this new query, your join to Book_Category happens based only upon the record set returned from the join between Publisher and Book. If there is filtering that happens based on this join, you will see a performance increase.
You also have the added benefit of being ANSI-compliant, as well as explicit coding to improve ease of maintenance.

What's wrong with my sql query using left joins?

I'm using MySQL. I have 3 tables I'm trying to connect in a query and I can't see what I'm doing wrong with the following query:
Table 1: books (list of book information)
Table 2: bookshelf (list of books a member owns)
Table 3: book_reviews (list of book reviews)
I want to generate a list of all books a user has in their bookshelf, as well as any reviews they have made. The following query gives me a list only of books the user has reviewed; I want all of their books. I thought the second LEFT OUTER JOIN would do this - connecting the bookshelf titles to the book reviews titles, but I don't get any books with no reviews (there should be lots). Removing the second JOIN statement (and putting bookshelf in the FROM statement) allows me to get a list of titles with no reviews, but it shows all book reviews by all users.
SELECT books.title, book_reviews.comments
FROM books
LEFT OUTER JOIN book_reviews ON books.ID = book_reviews.book_id
LEFT OUTER JOIN bookshelf ON book_reviews.user_id = bookshelf.user_id
WHERE bookshelf.book_id = books.ID
AND bookshelf.user_id =1
I imagine I'm missing something very obvious, but I've been reading about joins and going over my logic and I'm blind to it. Thanks to anyone who can help me see...
Try this:
SELECT books.title, book_reviews.comments
FROM bookshelf
LEFT OUTER JOIN books ON books.ID = bookshelf.book_id
LEFT OUTER JOIN book_reviews ON book_reviews.book_id = books.ID
AND book_reviews.user_id = bookshelf.user_id
WHERE bookshelf.user_id =1