Select t.Name, a.Questions, b.Correct,c.Attempted from Topics t
Left Join (
SELECT COUNT(TestResults2.QuestionID) AS Questions,
Topics.Name
FROM TestResults INNER JOIN
TestResults2 ON TestResults.ID = TestResults2.TestResultID
INNER JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID
INNER JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) a
On t.Name=a.Name
Inner Join (
SELECT COUNT(TestResults2.QuestionID) AS Correct,
Topics.Name
FROM TestResults INNER JOIN
TestResults2 ON TestResults.ID = TestResults2.TestResultID
INNER JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID
AND TestResults2.Answer = QuestionBank.Answer
INNER JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) b
On t.Name=b.Name
Inner Join (
SELECT COUNT(TestResults2.QuestionID) AS Attempted,
Topics.Name
FROM TestResults INNER JOIN
TestResults2 ON TestResults.ID = TestResults2.TestResultID
INNER JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID
AND TestResults2.Answer <> '\0'
INNER JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) c
on c.Name=t.Name
答案 0 :(得分:0)
SELECT t.Name, a.Questions, b.Correct,c.Attempted
FROM Topics t
LEFT JOIN (
SELECT COUNT(TestResults2.QuestionID) AS Questions,
Topics.Name
FROM TestResults
JOIN TestResults2 ON TestResults.ID = TestResults2.TestResultID
JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID
JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) a On t.Name=a.Name
LEFT JOIN (
SELECT COUNT(TestResults2.QuestionID) AS Correct,
Topics.Name
FROM TestResults
JOIN TestResults2 ON TestResults.ID = TestResults2.TestResultID
JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND TestResults2.Answer = QuestionBank.Answer
JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) b On t.Name=b.Name
LEFT JOIN (
SELECT COUNT(TestResults2.QuestionID) AS Attempted,
Topics.Name
FROM TestResults
JOIN TestResults2 ON TestResults.ID = TestResults2.TestResultID
JOIN QuestionBank ON TestResults2.QuestionID = QuestionBank.ID AND TestResults2.Answer <> '\0'
JOIN Topics ON QuestionBank.TopicID = Topics.ID
WHERE TestResults.StudentID = 1
AND TestResults.ID = 46
GROUP BY Topics.Name) c on c.Name=t.Name
这是你的查询,我只是用LEFT JOIN更改了一些JOINS,检查你是否能够处理NULL值。