I have 2 tables:
authors table:
id_author
author(author name in this column)
author_ind table:
id_book
id_author
My query is:
SELECT author
FROM authors
LEFT JOIN author_ind
ON author_ind.id_book = author_ind.id_author
LEFT JOIN authors author
ON author_ind.id_author = authors.id_author
WHERE author_ind.id_book=%d
I need to get the authors info from the authors table based on the id_book from the author_ind table. MYSQL error #1052 - Column 'author' in field list is ambiguous.
Little help please:)
Update: thank you everyone. correct query:
SELECT authors.author
FROM authors, author_ind
WHERE author_ind.id_book =14
AND authors.id_author = author_ind.id_author
14 is just a book id, I used it to test if it worked instead of %d
Based on what you're trying to get, you've made this more complex than it needs to be. You don't need to do a 2nd join on the authors table. Also, if you only want authors who have a record in the author_ind table, you can do a INNER JOIN instead of LEFT JOIN:
SELECT author
FROM authors
INNER JOIN author_ind
ON authors.id_author = author_ind.id_author
WHERE author_ind.id_book=%d
Related
OBJECTIVE: Want to show books, book like count, authors, and author like count.
-- books: start
SELECT * FROM books
-- likes: get like count of books
JOIN likes ON likes.id = books.likes_id
-- merge table: join to book
JOIN authors_books ON books.id = authors_books.book_id
-- authors: join merge table
JOIN authors ON authors.id = authors_books.author_id
-- likes: get like count of authors
JOIN ????????
I'm stuck at authors likes.
Instead of likes_id:
I would like likes_count (see RESULTS GRID image).
I tried:
JOIN likes AS l2 ON likes.id = authors.likes_id
No luck. Shows all the correct column info at the top, but blank output.
You have to join with likes twice, once for the book, another for the author.
Since you join with the same table, you need to give them aliases to distinguish them.
SELECT books.id, books.title, books.pages, book_likes.count AS book_likes,
authors.first_name, author.last_name, author_likes.count AS author_likes
FROM books
JOIN likes AS book_likes ON book_likes.id = books.likes_id
JOIN authors_books ON books.id = authors_books.book_id
JOIN authors ON authors.id = authors_books.author_id
JOIN likes AS author_likes ON author_likes.id = authors.likes_id
Say I have a database of publishers, who employ authors, who write books.
Or to phrase it another way, each book, is written by an author, who works for a publisher.
publishers: id
authors: id, publisher_id
books: id, author_id
I know how to get a list of publishers with how many authors each employs, from this question.
How do I get a list of publishers with how many books each has published?
How can I get both - publishers, each with number of authors and number of books?
try this
SELECT COUNT(DISTINCT b.`id`) noofbooks,COUNT(DISTINCT au.id) noofauthers,pub.id publisher FROM publisher pub
INNER JOIN auther au ON au.`pub_id`= pub.`id`
INNER JOIN books b ON b.`aut_id` = au.`id` GROUP BY pub.id
You need a three table join
SELECT publisher.id, count(*) from publisher
INNER JOIN author on publisher.id = author.publisher_id
INNER JOIN book on author.id = book.author_id GROUP BY publisher.id;
You just need to fire a simple sql join query for that like as follow.
SELECT p.publishers , COUNT(a.authors) totalAuthors, COUNT(b.books) TotalBooks
FROM publishers AS p,authors AS a ,books AS b
WHERE p.publishersid = a.publishersid
AND a.authorsid = b.authorsid
GROUP BY p.publishersid;
I ended up with something similar to bhanu's answer:
SELECT publishers.*,
COUNT(DISTINCT authors.id) AS 'author_count',
COUNT(DISTINCT books.id) AS 'book_count'
FROM publishers
LEFT JOIN authors ON (authors.publisher_id = publishers.id)
LEFT JOIN books ON (books.author_id = authors.id)
GROUP BY publishers.id;
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%'
I have 3 tables in a MySQL database,
Author
Book
Author_has_Book.
The author has two columns
idauthor
name
Book also has two columns
idbook
name
Author_has_Book also has two columns
foreign keys of an author and a book book_idbook
author_idauthor.
I have successfully inserted an author and a book into both tables and I have entered their keys into the join table.
Now how do I use this join table to get all books by a certain author, or all authors for a book? Is this accomplished with joins?
edit: The suggested duplicate is not the same question.
It will get you all records
SELECT Author.*
FROM Author Author
INNER JOIN Author_has_Book AuthorHasBook
ON Author.idauthor = AuthorHasBook.author_idauthor
INNER JOIN Book Book
ON AuthorHasBook.book_idbook = Book.idbook
and if you wish to select any specific author, you just need to mention it in where clause, see example below:
SELECT Author.*
FROM Author Author
INNER JOIN Author_has_Book AuthorHasBook
ON Author.idauthor = AuthorHasBook.author_idauthor
INNER JOIN Book Book
ON AuthorHasBook.book_idbook = Book.idbook
WHERE Author.idauthor = 'your author id'
Try this query
select a.name,c.name from book a
join Author_has_Book b on a.idbook=b.idbook
join author c on b.idauthor=c.idauthor
I'm stuck with creating a MySQL query. Below is my database structure.
authors (author_id and author_name)
books (book_id and book_title)
books_authors is the link table (book_id and author_id)
Result of all books and authors:
I need to get all the books for certain author, but if a book has 2 authors the second one must be displayed also. For example the book "Good Omens" with book_id=2 has two authors. When I run the query I get the books for the author_id=1 but I can not include the second author - "Neil Gaiman" in the result. The query is:
SELECT * FROM books
LEFT JOIN books_authors
ON books.book_id=books_authors.book_id
LEFT JOIN authors
ON books_authors.author_id=authors.author_id
WHERE books_authors.author_id=1
And below is the result:
You need to change the WHERE clause to execute a subselect like this:
SELECT b.*, a.*
FROM books b
LEFT JOIN books_authors ba ON ba.book_id = b.book_id
LEFT JOIN authors a ON a.author_id = ba.author_id
WHERE b.book_id IN (
SELECT book_id
FROM books_authors
WHERE author_id=1)
The problem with your query is that the WHERE clause is not only filtering the books you are getting in the result set, but also the book-author associations.
With this subquery you first use the author id to filter books, and then you use those book ids to fetch all the associated authors.
As an aside, I do think that the suggestion to substitute the OUTER JOINs with INNER JOINs in this specific case should apply. The first LEFT OUTER JOIN on books_authors is certainly useless because the WHERE clause guarantees that at least one row exists in that table for each selected book_id. The second LEFT OUTER JOIN is probably useless as I expect the author_id to be primary key of the authors table, and I expect the books_authors table to have a foreign key and a NOT NULL constraint on author_id... which all means you should not have a books_authors row that does not reference a specific authors row.
If this is true and confirmed, then the query should be:
SELECT b.*, a.*
FROM books b
JOIN books_authors ba ON ba.book_id = b.book_id
JOIN authors a ON a.author_id = ba.author_id
WHERE b.book_id IN (
SELECT book_id
FROM books_authors
WHERE author_id=1)
Notice that INNER JOINs may very well be more efficient than OUTER JOINs in most cases (they give the engine more choice on how to execute the stament and fetch the result). So you should avoid OUTER JOINs if not strictly necessary.
I added aliases and removed the redundant columns from the result set.
You don't need a subquery for this:
SELECT *
FROM book_authors ba
JOIN books b
ON b.book_id = ba.book_id
JOIN book_authors ba2
ON ba2.book_id = b.book_id
JOIN authors a
ON a.author_id = ba2.author_id
WHERE ba.author_id = 1
You're pretty close... basically you need to identify all unique book ids for which author_id = ?. Then join that with the book_author table again to get all of the authors associate with those book ids. Then join to books and authors to get your book and author names.
Hopefully the following is very clear in this regard, but if it's not just let me know and I'll help explain it in more detail
SELECT a.*, d.* FROM books as a
INNER JOIN (SELECT book_id FROM books_authors WHERE author_id=?) as b
ON a.book_id=b.book_id
INNER JOIN books_authors as c
ON b.book_id=c.book_id
INNER JOIN authors AS d
ON d.author_id = c.author_id
Btw you could also structure this with a WHERE EXISTS clause. I don't think you'll see much of a performance difference either way, but just FYI you can try that if need be. Use EXPLAIN to view the execution plan for the query. If it's problematic, there are other ways to skin this cat.
Also, make sure you pay attention to indices. Whether you use the method here, or the method described by Frazz, a compound/mutli-column/complex index may make a big difference for you. That is, consider indexing books_authors by both book_id and by (author_id, book_id). Whether you should use an additional join or an IN or an EXISTS subquery... lots of ways to skin the cat. No matter what, though, having a multicolumn index on books_authors is likely to help you out, especially if this table is large