對於所有問題
SELECT a.userId, q.qid
,SUM(CASE WHEN q.answer = a.answer THEN 1 ELSE 0 END) AS correct_answers
,SUM(CASE WHEN q.answer = a.answer THEN 0 ELSE 1 END) AS wrong_answers
FROM questions q
JOIN answers a ON q.qid = a.qid
GROUP BY a.userId, q.qid
對於一個具體的問題
SELECT a.userId
,SUM(CASE WHEN q.answer = a.answer THEN 1 ELSE 0 END) AS correct_answers
,SUM(CASE WHEN q.answer = a.answer THEN 0 ELSE 1 END) AS wrong_answers
FROM questions q
JOIN answers a ON q.qid = a.qid
WHERE q.qid = <question_id>
GROUP BY a.userId
這將是非常有益的如果您可以提供表格模式以及如何在表格中設置這些列。 – rajasaur