简体   繁体   中英

Auto Updating mysql column according to Value of other columns

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 .

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

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;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM