1

私はこのクエリを持っています:

SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS `right_answers_num` 
FROM `completed` 
INNER JOIN `history` ON `history`.`ID` = `completed`.`ID` 
INNER JOIN `questions` ON `questions`.`ID` = `history`.`question`
WHERE `completed`.`student_id` = '1' AND `questions`.`answer` = `history`.`answer`       
GROUP BY `completed`.`ID` 
ORDER BY `completed`.`completed_in` DESC

必要なのは、完了したテーブル(id、level、completed_in、right_answer_num)で各テストの情報を取得することです。
そのクエリの問題は、正しい答えが1つもない場合(history.answer = question.answer)、そうではないことです。行を返しますが、row(id、level、completed_in)を返し、right_answer_num(counter)はゼロにする必要があります。

助けてください。ありがとうございます。

4

3 に答える 3

4
SELECT 
    completed.ID AS ID,
    completed.level AS level,
    completed.completed_in AS completed_in, 
    COUNT(questions.answer) AS right_answers_num 
FROM completed
        INNER JOIN history  ON history.ID = completed.ID
        LEFT JOIN questions ON questions.ID = history.question AND questions.answer = history.answer
WHERE 
    completed.student_id = '1'  
GROUP BY 
completed.ID 
ORDER BY completed.completed_in DESC
于 2012-06-19T13:10:50.937 に答える
1

LEFT OUTER JOINの代わりに を使用しINNER JOINます。

于 2012-06-19T13:08:17.797 に答える
0

2 番目の内部結合は、質問テーブルにレコードのない行が省略される原因です。内部結合は、対応するすべてのテーブルにデータがある行のみを返します。次のように、2 番目の内部結合を左結合に変更します。

SELECT 
    completed.ID AS ID,
    completed.level AS level,
    completed.completed_in AS completed_in, 
    COUNT(questions.answer) AS right_answers_num 
FROM completed
INNER JOIN history ON history.ID = completed.ID 
LEFT JOIN questions ON questions.ID = history.question
WHERE completed.student_id = 1       
GROUP BY completed.ID 
ORDER BY completed.completed_in DESC
于 2012-06-19T13:08:38.577 に答える