我有两张桌子
表格1 -mdl_question_attempts
id questionid rightanswer responsesummary
1 1 A A
2 1 A B
3 1 A A
4 1 A B
5 2 A A
6 1 A A
7 2 D E
8 2 D D
9 2 D E
10 3 F F
11 3 F G
表 2 -mdl_question_attempt_steps
id questionattemptid userid
5 1 1
6 2 1
7 3 2
8 4 1
9 5 2
10 6 1
11 7 1
12 8 1
13 9 1
14 10 1
15 11 1
表 1 - mdl_question_attempts
,主键 -id
字段与
表2- mdl_question_attempt_steps
、外键-questionattemptid
表 1 是关于用户对某些问题的回答。
rightanswer
- 是特定问题的正确答案,responsesummary
是用户针对该问题给出的答案。“questionid”代表问题编号。有时同一用户多次尝试一个问题,每次尝试的答案如表 1 所示。
对于每个问题,“userid”或用户可以从表 2 中找到
Eg: 1st row in table1 done by userid =1
所以我的问题是我想根据学习者两次回答问题的次数来找出学习者(一个用户,例如:userid = 1)两次错误回答同一个问题的百分比或比率?
表 1 中高亮显示的是 userid=1 的相关数据
用户 1 回答了问题 1 – 4 次,错误 2 次
用户 1 回答了问题 2 – 3 次,错误 2 次
问题 3 回答了 2 次,只答错了 1 次。所以我想要同样的问题两次错误。所以
不考虑问题 3
questionid wrong count
1 2/4
2 2/3
所以我的最终输出userid=1
是
=((2/4)+(2/3))/2
=0.583
=错误计数的总和除以平均值或为 2 次(仅回答 2 个问题)如果回答了 3 个问题,则总和应除以 3。
我写了以下三个代码,我可以分别得到输出。但我想在一个查询中得到这个
function quiztwicewrong()
{
$con=mysqli_connect("localhost:3306","root","", "moodle");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//quiz twice wrong
查询 1
$resultq = mysqli_query ($con,"SELECT mdl_question_attempts.rightanswer as rightanswer,count(mdl_question_attempts.questionid) as questionid1 FROM mdl_question_attempts,mdl_question_attempt_steps WHERE mdl_question_attempt_steps.questionattemptid=mdl_question_attempts.id and mdl_question_attempt_steps.userid='1' and mdl_question_attempts.rightanswer<>mdl_question_attempts.responsesummary GROUP BY mdl_question_attempts.questionid HAVING questionid1>1 ") or die("Error: ". mysqli_error($con));
while($rowq= mysqli_fetch_array( $resultq))
{
echo $rowq['questionid1']."-".$rowq['rightanswer']."<br>"."<br>"."<br>";
}
查询 2
$resultqall = mysqli_query ($con,"SELECT mdl_question_attempts.rightanswer as rightanswer,count(mdl_question_attempts.questionid) as questionid1 FROM mdl_question_attempts,mdl_question_attempt_steps WHERE mdl_question_attempt_steps.questionattemptid=mdl_question_attempts.id and mdl_question_attempt_steps.userid='1' GROUP BY mdl_question_attempts.questionid HAVING questionid1>1") or die("Error: ". mysqli_error($con));
while($rowqall= mysqli_fetch_array( $resultqall))
{
echo $rowqall['questionid1']."-".$rowqall['rightanswer']."<br>"."<br>"."<br>";
}
//query 3
$resultqdup = mysqli_query ($con,"SELECT count(*) as duplicate FROM
(select mdl_question_attempts.rightanswer as ightanswer from mdl_question_attempts,mdl_question_attempt_steps WHERE mdl_question_attempt_steps.questionattemptid=mdl_question_attempts.id and mdl_question_attempt_steps.userid='1' and mdl_question_attempts.rightanswer<>mdl_question_attempts.responsesummary GROUP BY mdl_question_attempts.questionid HAVING COUNT(mdl_question_attempts.questionid)>1) as questionid1 ") or die("Error: ". mysqli_error($con));
while($rowqdup= mysqli_fetch_array( $resultqdup))
{
echo $rowqdup['duplicate'];
}
mysqli_close($con);
}
return quiztwicewrong();
3 个查询的输出是
查询 1- 输出
2-A
2-D
查询 2- 输出
4-A
3-D
2-F (I don’t want this part-this comes for the 3rd question, but I want only the output related to query 1- ouput,only answer more than 1 time wromg)
查询 3- 输出
2
所以我想结合3个输出,需要计算并得到值
=((2/4)+(2/3))/2
=0.583
请通过编辑我的代码或任何建议来帮助我做到这一点?