I would appreciate your help greatly,I’m using php and mysql and I’m getting some unrealistic results, can you check my code if it is alright…btw the tables involved are
BOOKS
id, book_title, isbn, publisher, price, l1_subject_id, l2_subject_id,
description1, description2, description3, call_date, chapter_proposal_date,
notification_acceptance_date, publishing_fee_date, final_manuscript_date,
1st_proof_reading, 2nd_proof_reading, book_schedule_date, active, url,
pages_num, edited_by, downloaded_num, file_size, unix_name, voted,
general_notes, BMcomment, description4, about_the_book, call_started,
kw_mandatory, kw_other, ignore_small, show_contributing_authors
BOOKS_CHAPTERS
id, users_id, books_id, order, books_sections_id, manuscript_title, price,
active, paypending, notice, created_at, last_modified, keywords, status,
book_editor_comments, hard_copy, invoiceing_data, extended_deadline,
next_deadline, technical_notice, number
SELECT
COUNT(b.book_title) as `Total number of books `,
COUNT(bc.manuscript_title) as ` Total number of chapters`,
#DATE_FORMAT(b.call_started, '%M %Y') as `Date`,
#DATE_FORMAT(b.call_started, '%Y-%m') as `Original date format`,
(COUNT(b.book_title)/COUNT(bc.manuscript_title)) as `Average chapter number by book per
month`,
b.id as book_id
FROM books b
JOIN books_chapters bc ON (b.id = bc.books_id)
WHERE b.call_started IS NOT NULL AND b.call_started != '0000-00-00'
#GROUP BY MONTH(b.call_started), YEAR(b.call_started),b.id
#ORDER BY YEAR(b.call_started) ASC, MONTH(b.call_started)ASC
You join on chapters, but you count on book titles.
That means that you count the title for each chapter in the total number of books (or per month, if you re-add the group by).
Use
count(distict b.book_title), or rathercount(distinct b.id)orcount(distinct b.isbn)to get the number of distinct books in this result set.Same goes for chapters, and there you should also use id, which is probably unique, auto-numbered. Chapter titles may be the same between books.