Fetching data from many-to-many relationships - mysql

I want to fetch the title from book table and the subject name from subject table for all the books of a certain author
Here is the tables relationships
I have tried to select the title and the subject from the tables but I can't get it to work with the many to many relationship
SELECT book_title,subject_name FROM book,subject WHERE $subject_ID = subject_ID INNER JOIN book_author ON author_ID = '$author_ID'
I thought about making two separate queries, if someone could help me on that I would be really glad.

Try this one:
SELECT book_title, subject_name
FROM Book
INNER JOIN Book_Author ON Book.book_ISBN = Book_Author.book_ISBN
INNER JOIN Author ON Book_Author.author_ID = Author.author_ID
INNER JOIN Subject ON Subject.subject_ID = Book.subject_ID
WHERE author_lastname = [whatever];
And nice job posting the model :)
Edit to match the exact need:
SELECT book_title, subject_name
FROM Book
INNER JOIN Book_Author ON Book.book_ISBN = Book_Author.book_ISBN
INNER JOIN Subject ON Subject.subject_ID = Book.subject_ID
WHERE author_ID = '11';
And by the way, you're having an "Column 'author_ID' in where clause is ambiguous" because this column appears both in Book_Author and Author. That's why you have to prefix it with the table name :)

Related

Mysql Joining data from 3 tables

I can't seem to figure out what join I need to do. I have 3 tables which have all the information. I've map out the steps but I'm unsure what to do now, I need to display all the books listed from a specific author.
There is 3 tables I need to use
Author - which contains author_code, fname & lname.
Book - which contains book_code, title, publisher_code, type, price and paperback
Wrote - which contains book_code, author_number and sequence
Here's the code so far, really I know I should use author name but i can't figure it out.
SELECT BOOK_CODE
FROM WROTE
WHERE AUTHOR_NUM = 20
LEFT JOIN AUTHOR
ON WROTE.AUTHOR_NUM = AUTHOR.AUTHOR_NUM;
You need list of book, so start with book table then join it with wrote to get specific author and then join to author to get author data
SELECT BOOK_CODE
FROM BOOK
INNER JOIN WROTE
ON WROTE.BOOK_CODE = BOOK.BOOK_CODE
AND WROTE.AUTHOR_NUM = 20
INNER JOIN AUTHOR
ON AUTHOR.AUTHOR_NUM = WROTE.AUTHOR_NUM
SELECT Book.book_code
FROM Book
LEFT JOIN WROTE ON WROTE.book_code = Book.book_code
LEFT JOIN AUTHOR ON AUTHOR.author_code = WROTE.author_number
WHERE AUTHOR.author_code IN (123,456)

MySQL how to join 3 tables when one doesn't have a common key

I have three tables and I need to join as follows.
INVENTORY
BOOK_CODE, BRANCH_NUM, ON_HAND
BOOK
BOOK_CODE, TITLE
AUTHOR
AUTHOR_NUM, AUTHOR_LAST
Basically, I need to select the author name, book title, and on-hand count for all books from Branch 4 (in inventory). The problem is, the AUTHOR table has to be reached through a fourth table.
WROTE
BOOK_CODE, AUTHOR_NUM
If all the tables had that BOOK_CODE, I could do it easy, but I can't figure out how to jam it all into one query. I've tried the following:
SELECT TITLE, AUTHOR_LAST, ON_HAND
FROM BOOK, AUTHOR, INVENTORY
WHERE BOOK.BOOK_CODE = INVENTORY.BOOK_CODE
AND AUTHOR_NUM IN
(SELECT AUTHOR_NUM
FROM WROTE
WHERE WROTE.BOOK_CODE = INVENTORY.BOOK_CODE)
AND BRANCH_NUM='4';
But this returns the wrong fields, so I don't know what I'm doing wrong.
You can "chain" your joins:
SELECT title, author_last, on_hand
FROM book b
JOIN inventory i ON b.book_code = i.book_code
JOIN wrote w ON b.book_code = w.book_code
JOIN author a ON a.author_num = w.author_num
WHERE branch_num = 4
You should try Joining it will be Helpful rather that Sub query
SELECT a.AUTHOR_LAST,d.TITLE,c.ON_HAND FROM WROTE a INNER JOIN AUTHOR b
ON a.AUTHOR_NUM=b.AUTHOR_NUM INNER JOIN INVENTORY c
ON c.BOOK_CODE=a.BOOK_CODE INNER JOIN BOOK d
ON a.BOOK_CODE=d.BOOK_CODE
WHERE c.BRANCH_NUM=4
Try like this

I want to join five tables in MySQL

Books
bookid (PK)
title
price
author
authored (PK)
authorname
publisher
pubid (PK)
pubname
bookauthors
bookid (FK)
authorid
bookpublisher
bookid (FK)
pubid
I want to join all five tables and get these column values:
bookid
title
price
authorname
pubname
I should get only one row for a query to get above result. What I wrote was:
select books.*, authors.authorname
from authors,books
inner join bookauthors on books.bookid = bookauthors.bookid
inner join publisherbook on books.bookid = publisherbook.bookid
where books.bookid = 459567;
but instead I am getting many rows with duplicate data.
You need to use joins for all the tables. Using multiple tables in the FROM clause results in a Cartesian product. Try this
SELECT books.bookid, books.title, books.price, authors.authorname
FROM books
INNER JOIN bookauthors ON books.bookid = bookauthors.bookid
INNER JOIN publisherbook ON books.bookid = publisherbook.bookid
INNER JOIN authors ON bookauthors.authorid = authors.authorid -- assuming you meant authorid and not authored
WHERE books.bookid = 459567;
Of course, if your book has multiple authors, you will get multiple rows in your resultset.
I'm not sure why you're joining in publisherbook (or bookpublisher from your original list) unless you're just ensuring that your book has a publisher. Again, multiple publishers will result in even more (duplicate) results.
If you just want to make sure your book has at least one publisher, try this instead...
SELECT books.bookid, books.title, books.price, authors.authorname
FROM books
INNER JOIN bookauthors ON books.bookid = bookauthors.bookid
INNER JOIN authors ON bookauthors.authorid = authors.authorid -- assuming you meant authorid and not authored
WHERE EXISTS (
SELECT 1 FROM publisherbook
WHERE publisherbook.bookid = books.bookid
) AND books.bookid = 459567;

Getting data from multiple tables using joins

I have various tables like
Student
primary id , students name, course
Papers
paper id, papername, course, semester, type
StudentOptions
primary id, studentid (foreign key - reference student id) and paperid (foreign key - references paper id)
StudentsTerm
studentid (foreign key- references student id) and student semester
Now the kind of result i want is,
I want to choose a course then the term, which will give me the number of papers/subject it has with their types (Mandatory/Optional) and with that i want to have the count of number of students studying those papers from all these tables.
I don't wanna create any view or stuff, Just a normal select query will do.
The query i am running is :
SELECT p_name,
p_id,
type,
Count(sps.studentid) AS counts
FROM students,
str,
papers
LEFT JOIN sps
ON sps.paperid = papers.p_id
WHERE sps.studentid = students.studentid
AND students.studentid = str.studentid
AND sps.studentid = str.studentid
AND str.semesterid = p_semid
AND str.sessionid = 12
AND students.course = c_id
AND c_id = 6
AND p_semid = 1
GROUP BY p_id
As better practice, if you are going to be using explicit JOIN syntax, then don't use implicit syntax. In the query you posted above, you select from papers, but you don't use it anywhere. Also, your column names are slightly ambiguous. If it's easier, alias each table using a single letter, or explicitly prefix the column names. If you're using an aggregate with GROUP BY, you cannot select columns which are not in the group.
Let's assume this is your ER diagram:
Let's first join all the tables:
SELECT a.id, a.name
FROM student a
JOIN str b ON b.student_id = a.id
JOIN sps c ON c.student_id = a.id
JOIN papers d ON d.id = c.paper_id
Now you wish to find the number of students studying papers from a specific course and type:
SELECT a.id, a.name
FROM student a
JOIN str b ON b.student_id = a.id
JOIN sps c ON c.student_id = a.id
JOIN papers d ON d.id = c.paper_id
WHERE b.semester = 12
AND d.course = 6
Because your attributes are ambigiuous, it is hard to tell what tables they are coming from. If you can set up the structure and sample data on SQL Fiddle, we could help you better.

Struggling with MySQL query using joins

I have 3 tables for storing information about books:
books
-----
id
title
authors
-------
id
name
books_to_authors
----------------
book_id
author_id
When a book's information is displayed, I then want to be able to select any other books by the same authors.
I have the current book id available from the first query, but I can't figure out where to start to achieve what I need as there can be multiple authors. Obviously with just one of them it would be simple, so I'm really struggling with this. Any help would be much appreciated!
I think this aught to do it. Just replace the ? with the book ID they are currently viewing and this will give you all the books by the same author.
SELECT b.*
FROM books b
INNER JOIN books_to_authors b2a ON b2a.book_id = b.id
WHERE b2a.author_id IN (
SELECT author_id FROM books_to_authors WHERE book_id = ?
)
If you want to exclude the book they are currently viewing, you can change the query like this:
SELECT b.*
FROM books b
INNER JOIN books_to_authors b2a ON b2a.book_id = b.id
WHERE b2a.author_id IN (
SELECT author_id FROM books_to_authors WHERE book_id = ?
)
AND b.id <> ?
$book_id = (your code for retrieving book_id);
$db_query = "SELECT b.*
FROM books b
INNER JOIN books_to_authors bta ON bta.book_id = b.id
WHERE bta.author_id IN (
SELECT author_id FROM books_to_authors WHERE book_id = ".$book_id."
)";
I presumed that you are using php. If I'm wrong, just use SQL query string, and ignore the rest...
You're looking for the query below. I see some solutions with subqueries and I'd highly recommend not using subqueries. They are slower than running 2 queries:
Having the book id you do SELECT author_id FROM books_to_authors WHERE book_id = '{$book_id}'
Get the author id and then run this:
SELECT books.id, books.title, authors.name FROM books RIGHT JOIN books_to_authors ON books_to_authors.book_id = books.id) RIGHT JOIN authors ON (authors.id = books_to_authors.author_id) WHERE authors.id = '{$author_id}'