我非常感谢您的帮助,我正在使用php和mysql,并且得到了一些不切实际的结果,您能否检查我的代码是否还行...所涉及的表是

图书

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

最佳答案

您参加了章节,但您依靠的是书名。
这意味着您要在总书籍数(或每月,如果您重新添加分组依据)中计算每个章节的标题。

使用count(distict b.book_title)count(distinct b.id)count(distinct b.isbn)来获取此结果集中不同书籍的数量。

章节也一样,在那里您还应该使用id,它可能是唯一的,自动编号的。书籍之间的章节标题可能相同。

关于php - MySQL查询返回每月平均每本书的章节数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7606733/

10-13 08:49