sql >> Database teknologi >  >> RDS >> Mysql

Sådan får du sql WHERE-erklæring automatisk opdatering fra localhost-link

Brug denne erklæring til at få alle elevresultater i din 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;

Du kan oprette en PHP-variabel til at beregne procentdelen. For eksempel:

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

eller

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

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

Brug denne SQL-sætning til at få scorePercentage nu:
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 med ovenstående resultater her:http://sqlfiddle.com/#!9/8d47c/10



  1. Effektiviteten af ​​forskellige fejlhåndteringsteknikker

  2. Sideinddeling ved hjælp af PDO med MySQL

  3. Hvordan udvider jeg kommaseparerede værdier til separate rækker ved hjælp af SQL Server 2005?

  4. AssertionError:databaseforbindelse er ikke indstillet til UTC