1

你好我想问一些事情

如何使用mysql QUERY添加ABCD之类的分数

SELECT dcm_peserta.nm1, COUNT(siswa.nis) as jml, ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) as percent

        /* add SELECT for scoring here */
FROM dcm_peserta, siswa
WHERE siswa.id_kelas = dcm_peserta.id_kelas AND dcm_peserta.id_kelas = '65'
GROUP BY dcm_peserta.nis

就像如果百分比 = 0得分 A,如果百分比 = 1 直到 20得分 B

任何想法?

4

2 回答 2

3

只需使用字典查找成绩php

$grades = array ( -1=>'A', 'B', 'C', 'D','E','F' );

for ($percentage=0; $percentage<=100; $percentage++) {
  $grade = $grades[floor(($percentage-1)/20)];
  echo sprintf("%s%% => %s\n", $percentage, $grade);
}
于 2013-08-16T04:19:20.993 回答
2

如果您必须在查询中执行此操作,请使用CASE

请注意,这些案例的处理方式与结构的处理方式相同,因此您if...elseif不需要明确说明范围(即我>80在是,和。)=100'F'=100>80<> 100

SELECT ...
    CASE WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) = 100 THEN 'G'
         WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) > 80 THEN 'F'
         WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) > 60 THEN 'E'
         WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) > 40 THEN 'D'
         WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) > 20 THEN 'C'
         WHEN ROUND(dcm_peserta.nm1 / COUNT(dcm_peserta.nis) * 100) > 0 THEN 'B'
         ELSE 'A'
    END AS score
FROM ...
于 2013-08-16T04:17:49.913 回答