My database structure looks like this:
https://docs.google.com/open?id=0B9ExyO6ktYcOenZ1WlBwdlY2R3c
Explanation for some of tables:
answer_chk_results– checked answers table. So if some answer doesn’t exist on this table, it means it’s uncheckedlesson_questions– lesson <-> question associations (by id) table
When I query database like this:
SELECT
q.id,
q.content,
q.type
FROM
`questions_and_exercises` q,
`lesson_questions` lq
WHERE
q.id = lq.qid
AND lq.lid = 1
I am getting all list of questions. But what I would like is a count of answers and checked answers for every question. When I use this query:
SELECT
q.id,
q.content,
q.type,
COUNT(DISTINCT a.ID) answer_count,
COUNT(DISTINCT acr.id) checked_count
FROM
`questions_and_exercises` q,
`lesson_questions` lq
LEFT JOIN answers a ON a.qid = lq.qid,
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
q.id = lq.qid
AND lq.lid = 1
The results is only 1 question with the wrong count of answers. (There are about 9-10 questions in my DB) What am I missing?
A
group bywould help, like:Note that
count(acr.checked)returns the number of answers with a row in theanswer_chk_resultstable. Andcount(distinct acr.checked)returns the number of different values of the checked column. If checked is a boolean, that would always be one or zero.