1

I am working on students management system with php it this application i want to Update Students Class Roll no. According to their percentage Here is my sifo table

Sid   ||   Sname ||  Class  || Roll ||percentage 
ABC1       Raj         1         1        81     
ABC2       RAJU        1         2        91  

AS I MENTIONED ABOVE I want my application to allot ABC2 Roll 1 And Abc1 to roll as per their percentage.

I dont have any idea how to achieve this so I didn't tryed any code so please help me.

Oh I forgot to add I want to do it via PHP because This process is going to happen once in a year .

Tushar
  • 167
  • 12
  • Are you talking about ranking rows by percentage and store it in roll field? – kiks73 Jun 04 '14 at 06:20
  • I just Want to get Percentages of all students in same class and then Update Roll field according to their percentage Ex: If i got max Percentage in class then my roll is 1 – Tushar Jun 04 '14 at 06:24
  • 1
    I think that you need a trigger on insert to do that. You can take a look at this question: http://stackoverflow.com/questions/5222044/column-calculated-from-another-column – kiks73 Jun 04 '14 at 06:26
  • Kiks73 bro that post isn't useful for me.. – Tushar Jun 04 '14 at 06:33

2 Answers2

0

What you are looking for is a "computed column". MSSQL and Oracle have these, but I can't find anything about it in MySQL.

I would create a view on the table to do that same thing. That way you can do the computation inside the view in a similar way to the answer in this post: Create a computed column based on another column in MySQL

Community
  • 1
  • 1
Jase L
  • 36
  • 3
0

Try this code.

<?php
$sid_array = array();
$results = mysql_query("SELECT * FROM table_name order by percentage desc");
while ($row = mysql_fetch_array($results)) {
    $sid_array[] = $row['Sid']; // take all sid in descending order according to percentage
}
$i = 1;
for($sid_array as $key=>$value)
{
$results = mysql_query("update table_name set roll_no='$i' where Sid='$value'");
$i=$i+1;
}
?>