Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment obtenir la mise à jour automatique de l'instruction SQL WHERE à partir du lien localhost

Utilisez cette déclaration pour obtenir tous les scores des élèves de votre test.

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;

Vous pouvez créer une variable PHP pour calculer le pourcentage. Par exemple :

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

ou

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

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

Utilisez cette instruction SQL pour obtenir scorePercentage maintenant :
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 avec les résultats ci-dessus ici :http://sqlfiddle.com/#!9/8d47c/10