要添加最后回答的二十个问题,您需要选择最后二十行,然后计算正确答案,但是GROUP BY
不要LIMIT
很好地结合在一起,除非您一次只检查一个类别,否则您不能附加最后二十行. 当子查询之一引用正在连接的表时,MySQL 不允许您连接表。
因此,下面的查询是一种解决方法,它获取按时间戳排序的类别的所有答案,制作一个列表,取前 20 个,然后计算正确答案的数量。棘手,但完成工作。
SELECT category_id,
Total_Q_Tried,
Total_Unique_Q_Tried,
Total_Answered_Correctly,
Total_Answered_Correctly / Total_Q_Tried*100 Total_Correct_Answer_Percentage,
Total_Answered_Correctly_In_Last20,
Total_Answered_Correctly_In_Last20 / LEAST(20,Total_Q_Tried)*100 Total_Correct_Answer_Last20_Percentage
FROM (
SELECT
B.category_id, COUNT(B.question_id) Total_Q_Tried,
COUNT(DISTINCT B.question_id) Total_Unique_Q_Tried,
SUM(A.correct) Total_Answered_Correctly,
(SELECT length(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20))
- length(replace(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20),'1', ''))
FROM tableA AA INNER JOIN tableB BB ON AA.question_id = BB.question_id
WHERE BB.category_id = B.category_id
AND AA.user_id = A.user_id
) Total_Answered_Correctly_In_Last20
FROM tableA A LEFT JOIN tableB B
ON B.question_id = A.question_id
WHERE A.user_id = 34
GROUP BY B.category_id ) FinalNumbers
如果您想要最后 20 个正确答案的百分比,则需要使用 20 中的较小值,TOTAL_Q_TRIED
并且TOTAL_ANSWERED_CORRECTLY_IN_LAST20
在查询中计算得出。
--
我无法尝试,但如果有很多很多行,性能可能会不好。
| USER_ID | QUESTION_ID | DATE_ANSWERED | CORRECT |
|---------|-------------|--------------------------------|---------|
| 66 | 1 | January, 01 2013 00:00:00+0000 | 1 |
| 34 | 1 | January, 02 2013 00:00:00+0000 | 1 |
| 34 | 2 | January, 03 2013 00:00:00+0000 | 1 |
| 34 | 3 | January, 04 2013 00:00:00+0000 | 0 |
| 34 | 4 | January, 05 2013 00:00:00+0000 | 1 |
| 34 | 6 | January, 06 2013 00:00:00+0000 | 0 |
| CATEGORY_ID | QUESTION_ID |
|-------------|-------------|
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 3 | 6 |
| CATEGORY_ID | TOTAL_Q_TRIED | TOTAL_UNIQUE_Q_TRIED | TOTAL_ANSWERED_CORRECTLY | TOTAL_CORRECT_ANSWER_PERCENTAGE | TOTAL_ANSWERED_CORRECTLY_IN_LAST20 | TOTAL_CORRECT_ANSWER_LAST20_PERCENTAGE |
|-------------|---------------|----------------------|--------------------------|---------------------------------|------------------------------------|----------------------------------------|
| 1 | 1 | 1 | 1 | 100 | 1 | 100 |
| 2 | 3 | 3 | 2 | 66.6667 | 2 | 66.6667 |
| 3 | 1 | 1 | 0 | 0 | 0 | 0 |
根据下面的评论 - 添加正确回答的独特问题的总数。
这变得越来越艰难。我加入每一列,包括添加的最新查询中的时间戳以获得唯一答案。见下文。
SELECT category_id,
Total_Q_Tried,
Total_Unique_Q_Tried,
Total_Answered_Correctly,
Total_Unique_Answered_Correctly,
Total_Answered_Correctly / Total_Q_Tried*100 Total_Correct_Answer_Percentage,
Total_Answered_Correctly_In_Last20,
Total_Answered_Correctly_In_Last20 / LEAST(20,Total_Q_Tried)*100 Total_Correct_Answer_Last20_Percentage
FROM (
SELECT
B.category_id, COUNT(B.question_id) Total_Q_Tried,
COUNT(DISTINCT B.question_id) Total_Unique_Q_Tried,
SUM(A.correct) Total_Answered_Correctly,
SUM(UniqueA.correct) Total_Unique_Answered_Correctly,
(SELECT length(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20))
- length(replace(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20),'1', ''))
FROM tableA AA INNER JOIN tableB BB ON AA.question_id = BB.question_id
WHERE BB.category_id = B.category_id
AND AA.user_id = A.user_id
) Total_Answered_Correctly_In_Last20
FROM tableA A LEFT JOIN tableB B
ON B.question_id = A.question_id
LEFT JOIN (select user_id, question_id, MAX(date_answered) date_answered, correct
from tableA
GROUP BY user_id, question_id, correct
) UniqueA
ON A.user_id = UniqueA.user_id AND A.question_id = UniqueA.question_id AND A.date_answered = UniqueA.date_answered
WHERE A.user_id = 34
GROUP BY B.category_id ) FinalNumbers;
对于正确回答的最后 20 个问题的百分比,这可能不正确。请测试一下。如果它不替换,tableA A
andtableA AA
与UniqueA
's select 查询仅处理唯一答案并删除添加的最新左连接。