我对Laravel很新。
我有三个名为申请人,问题和答案的表:
申请人
id jobid name
1 151 ABC
2 151 DEF
答案
id qid jobid aaplicantid answer
1 21 151 1 1
2 22 151 1 0
3 21 151 2 1
4 22 151 2 1
问题
id jobid answer
21 151 1
22 151 1
在这里,我想根据问题计算申请人的分数百分比答案表只是假设一个工作有两个问题如果申请人答案与问题表答案匹配那么如果任何一个不匹配他将获得100%分数从分数中减去那么多。
我希望得分应该像 例如:一份工作有5个问题
score=>100/count(questions)=20 for each matched answer
对于jobid 151申请人名单应如下
id name score joid
1 ABC 50% 151
2 DEF 100% 151
我正在使用以下查询,但它没有给出预期的结果。
$apps=applicants::where('jobid','=',$jobId)->get();
foreach($apps as $apo){
$appid[]=$apo->id;
}
$score=answers::whereIn('appid',$appid)->where('jobid','=',$jobId)->get();
$qid=array();
foreach($score as $s){
$qid[]=$s->qid;
}
$queid=questions::whereIn('id',$qid)->where('jobid','=',$jobId)->get();
$calscore=100/(count($queid));
$key = 0 ; $finalscore = array();$sss = 0;
for($n = 0; $n< count($queid); $n++) {
foreach($queid as $que){
$quei[]=$que->id;
$weight[]=$que->weight;
foreach ($score as $s){
$qid[]=$s->qid;
$sc[]=$s->answer;
if($que->id==$s->qid){
if($weight==$sc){
$finalscore=$sss+$calscore;
$sss++;
}
}
}
$key++;
}
}
dd($finalscore);
甚至我尝试使用连接查询它没有给出正确的结果。有说服力的模型有可能吗?
请有人帮我解决这个问题。
答案 0 :(得分:0)
我建议您使用MySQL语句来计算百分比。
您可以在Laravel MySQL数据库中创建一个视图。
SELECT *, (SUM(answer) / COUNT(*) * 100) AS results
FROM answers
LEFT JOIN applicants
ON answers.applicantid = applicants.id
GROUP BY answers.applicantid
也可以从Laravel中调用它,如:
$results = DB::select(DB::raw("
SELECT *, (SUM(answer) / COUNT(*) * 100) AS results
FROM answers
LEFT JOIN applicants
ON answers.applicantid = applicants.id
GROUP BY answers.applicantid
"))
正如您所看到的,根据您的示例,它会返回50%和100%的结果:
注意: