MySql查询以每月的书数返回平均章节数

时间:2011-09-30 06:34:21

标签: php mysql

我非常感谢你的帮助,我正在使用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

1 个答案:

答案 0 :(得分:1)

你加入章节,但你依靠书名。 这意味着您计算书籍总数中每章的标题(如果您重新添加该组,则每月计算一次)。

使用count(distict b.book_title),或者更确切地说count(distinct b.id)count(distinct b.isbn)来获取此结果集中不同图书的数量。

章节也是如此,你也应该使用id,它可能是唯一的,自动编号的。书籍之间的章节标题可能相同。