2011-09-30 11 views
0

私はPHPとMySQLを使用していますが、私はいくつかの非現実的な結果を得ています。ところで関与のテーブルがあるMySqlクエリーで月ごとのブックごとの平均チャプター数を返します

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 
+1

あなたはどのような非現実的な結果を得ていますか? – Ehtesham

+0

あなたは何を得ていますか?あなたが欲しいもの ? – Rikesh

+0

ここで*正確に*問題がありますか? –

答えて

1

あなたはチャプターに参加しますが、あなたは本のタイトルに数えられます。 これは、書籍の総数(またはグループを再度追加した場合は月)の各章のタイトルを数えることを意味します。

count(distict b.book_title)、またはcount(distinct b.id)またはcount(distinct b.isbn)を使用して、この結果セット内の別個の書籍の数を取得します。

同じことがチャプターになりますが、おそらく一意で、自動的に番号が付けられたidも使用する必要があります。章のタイトルは書籍間で同じでもよい。

+0

チャームのように働いた!ありがとうございました!スタックオーバーフローのための愛は、それはnoobsの夢! –

関連する問題