MySQL query: Trying to join likes - mysql

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

Related

Join statement with multiple conditions

Ok I have two mysql table one called books and the other reading_tracking.
I have written the join query below:$query = "SELECT * FROM books inner join reading_tracking on book_num = book_id;
My dilemma is that i don't want all the from books i want a query which in theory would look something like this:
"SELECT * FROM books
where reading_status = 1 inner
join SELECT * FROM reading_tracking
on book_num = book_id";
If i try this as suggested:
SELECT * FROM books b
INNER JOIN reading_tracking rt
ON b.book_id = rt.book_num
WHERE rt.reading_status = 1;
The thing is in my program not all books in books table are in reading_tracking, user have to make an additional step, so i don't want books that are similar in both tables i want all the books from books where reading_status = 1; but want all the books from reading_tracking.
Your syntax should have the SELECT [x] FROM followed by the INNER JOIN, and then you need an ON to denote which field is synonymous between the two tables. In your example, I'm assuming you have a column book_id in books, and a column book_num in reading_tracking.
The WHERE clause should come after the ON, and additionally should specify which of the two tables you want to look for the column in.
This can be seen in the following:
SELECT * FROM books b
INNER JOIN reading_tracking rt
ON b.book_id = rt.book_num
WHERE rt.reading_status = 1;
This will search books for any row that has a reading_status of 1 in the reading_tracking table.
I assume column book_num is from books table, column book_id is from reading_tracking table. First join the 2 tables then use WHERE to filter the condition:
SELECT * FROM books b
INNER JOIN reading_tracking r
ON b.book_num = r.book_id
WHERE b.reading_status = 1;
You can use a subquery before doing the JOIN as shown below:
SELECT *
FROM (SELECT * FROM books WHERE reading_status=1) A
INNER JOIN reading_tracking B on B.book_num=A.book_id;
See Using MySQL Alias To Make The Queries More Readable

MySQL: How to count references to references to a table?

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;

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%'

SQL Inner Join returns strange results

Ok, I have two tables in my database. I have a table books, that has (id, title, author), and I have another table orders which has (id, bookid, authorid, fromdate, todate).
I am just to get all the books, that are available to order/reserve. Meaning, all the books from the books table, where in the orders table there should be no orders.bookid the same as any books.id
So, I've made this query:
SELECT books.title, books.author
FROM books
INNER JOIN orders
ON orders.bookid ORDER <> books.id
looks like I've found it, looks to work with:
SELECT * FROM books LEFT JOIN orders ON books.id = orders.bookid WHERE orders.bookid IS NULL
What do you think ?
So, I have 6 books in my example, with ids 1-6, and I have in orders table two orders, where the booksid(s) are 3 and 4. Now, if I run my query it shows me the books from 1-2 and 5-6 (twice), and also the books 3 and 4 once.
I don't know exactly what I'm doing wrong, but anyway, I'm kind of stuck.
You can try this:
SELECT b.title, b.author
FROM books b
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.bookid = b.id);
Why not give your tables alias names and join on the id column?
SELECT
B.TITLE,
B.AUTHOR
FROM BOOKS B
INNER JOIN ORDERS O ON B.ID = O.BOOKID

How to describe columns to be selected from joined table

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