sql >> Base de Datos >  >> RDS >> Mysql

Cómo obtener la actualización automática de la declaración WHERE de sql desde el enlace localhost

Use esta declaración para obtener todos los puntajes de los estudiantes de su prueba.

SELECT sq.stdid, sq.testid, COUNT(*) AS correctAnswers, 
SUM(q.marks) AS studentScore, (SELECT SUM(marks) FROM question 
WHERE testid=$_REQUEST['testid']) AS totalScore 
FROM question q, studentquestion sq 
WHERE sq.testid=$_REQUEST['testid'] AND q.testid = sq.testid 
AND q.qnid = sq.qnid AND sq.answered = 'answered' 
AND q.correctanswer = sq.stdanswer GROUP BY sq.stdid;

Puede crear una variable PHP para calcular el porcentaje. Por ejemplo:

$scorePercentage = $r3['studentScore'] / $r3['totalScore'] * 100;

o

$scorePercentage = $r3['studentScore'] .'/'. $r3['totalScore'];

// Output: echo $scorePercentage;
60
60/100

Utilice esta instrucción SQL para obtener el porcentaje de puntuación ahora:
SELECT sq.stdid, sq.testid, COUNT(*) AS correctAnswers, 
(SUM(q.marks) / (SELECT SUM(marks) FROM question WHERE testid=1) * 100) 
AS studentScorePercentage FROM question q, studentquestion sq 
WHERE sq.testid=$_REQUEST['testid'] AND q.testid = sq.testid AND q.qnid = sq.qnid 
AND sq.answered = 'answered' AND q.correctanswer = sq.stdanswer 
GROUP BY sq.stdid;

SQLFiddle con los resultados anteriores aquí:http://sqlfiddle.com/#!9/8d47c/10