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;
Related
I have 3 Tables with a n:m Relation
Languages:
ID (primary-key)
name
slug
Products:
ID
name
slug
-productgroup
Table for Relation:
Lang_Prod:
lang_prod_id (composed from lang_id and prod_id)
lang_id
prod_id
Now i want to get all Products with their corresponding Language-Names:
product
productgroup
language-names (multiple values)
I've tried:
SELECT product.*, languages_products.language_id,
GROUP_CONCAT(languages_products.language_id) as languages
FROM product
INNER JOIN languages_products ON id = languages_products.product_id
GROUP BY product.id;
Result is:
productname
product_slug
product_group
String with languages IDs
Instead of the string with the language-IDs, i would like to have a string with language-names
How can i archieve that?
You need to join with language table
SELECT product.id,product.name,product.productgroup,
GROUP_CONCAT(l.name) as languages
FROM product
INNER JOIN languages_products ON id = languages_products.product_id
INNER JOIN languages l ON languages_products.language_id=l.id
GROUP BY product.id,product.name,product.productgroup
You need to add another join with the languages table, and use the name column from it. I'd group it in an inner query though:
SELECT p.*, languages
FROM product p
JOIN (SELECT product_id, GROUP_CONCAT(l.name) as languages
FROM languages_products lp
JOIN languages l ON lp.language_id = l.id
GROUP BY product_id) cl ON cl.product_id = p.id
I have the following four tables:
Subscription:
book_id, duration, subscriber
Book:
book_id, book_name
Genre:
genre_id, genre_name
book_to_genre:
genre_id, book_id
For a certain user (subscriber) I would like to get all rows from subscription. The book name should be fetched from table Book. I know this is done with an inner join:
SELECT Book.book_name, Subscription.duration from Subscription INNER JOIN Book on Subscription.book_id = Book.book_id where Subscription.subscriber = "somevalue";
What if I would like to fetch genre_name from table Genre, where Genre.genre_id = book_to_genre.genre_id for that book?
Here is a modified version of your initial query that will return genre_name:
- I added the field genre_name in the SELECT part
- I added table aliases to make it easier to read
- I added 2 INNER JOIN: one between tables book and book_to_genre, the other between tables book_to_genre and Genre
SELECT b.book_name, s.duration, g.genre_name
FROM Subscription s
INNER JOIN Book b on s.book_id = b.book_id
INNER JOIN book_to_genre bg ON b.book_id = bg.book_id
INNER JOIN Genre g ON bg.genre_id = g.genre_id
where s.subscriber = "somevalue";
Documentation:
- SELECT
- JOIN
I have 3 tables: books, book_categories, categories.
book_categories table "joins" books and categories. It contains columns: id,book_id,category_id.
So one Book may belong to many categories and one Categorie may have many books.
I need query which retrieves all books from given_category except books which belongs to given_set_of_categories. So for example I want all books from category A but only if they don't belong also to category B or C. I need also sort (order) the result by Book.inserted column.
I know how to get all books from given_category with 2 joins but can't figure out how to exclude some books from other categories in result. I cant filter books in PHP because I am paginating the search result.
where
category_id = <given category>
and books.book_id not in
(
select book_id from book_categories
where category_id in (<given set of cat>)
)
order by books.inserted
So, if you mean it is in one category but not in any other:
AND EXISTS(SELECT * FROM books b JOIN book_categories bc ON b.id = bc.book_id JOIN categories c ON bc.category_id = c.id AND c.id = 'A')
AND NOT EXISTS(SELECT * FROM books b JOIN book_categories bc ON b.id = bc.book_id JOIN categories c ON bc.category_id = c.id AND c.id != 'A')
I think that this can be achieved through counting provided that book_categories entries are unique, thus the combination book_id & category_id are not repeating. Instead of trying directly to exclude records, we select from the combined set of categories [,] and then we'll count book_id entries that belong to the :
COUNT(IF(category_id = <given_category>, 1, NULL)) as cnt_exists
and after ensuring that it contains the required category, we count the total to see if it belongs to any other category as well:
COUNT(*) AS cnt_total
SELECT * FROM books b JOIN (
SELECT book_id,
COUNT(IF(category_id = <given_category>, 1, NULL)) as cnt_exists,
COUNT(*) AS cnt_total FROM book_categories WHERE
category_id IN(<given_category>, <given_set_of_categories>)
) bc ON b.id = bc.book_id AND
cnt_exists = 1 AND cnt_total = 1 ORDER BY b.inserted
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 :)
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}'