Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Bagaimana cara mendapatkan pembaruan otomatis pernyataan sql WHERE dari tautan localhost

Gunakan pernyataan ini untuk mendapatkan semua nilai siswa dari ujian Anda.

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;

Anda dapat membuat variabel PHP untuk menghitung persentase. Misalnya:

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

atau

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

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

Gunakan pernyataan SQL ini untuk mendapatkan scorePercentage sekarang:
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 dengan hasil di atas di sini:http://sqlfiddle.com/#!9/8d47c/10



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL Gunakan pernyataan WHERE untuk menargetkan baris tertentu

  2. Menyimpan Tag di Database. Simpan tag sekali atau berkali-kali?

  3. Berapa banyak teks UTF-8 yang muat di bidang Teks MySQL?

  4. Tanggal penanganan masalah dalam doktrin php

  5. perulangan mysql_real_connect atau apa