简体   繁体   English

根据其他列的值自动更新mysql列

[英]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. 我正在使用php它管理学生管理系统,此应用程序我想更新Student Class RollNo。 According to their percentage Here is my sifo table 根据他们的百分比这是我的sifo表

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. 上面我提到过,我希望我的应用程序按照其百分比分配ABC2第1卷和Abc1。

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 . 哦,我忘了补充一点,我想通过PHP来完成它,因为此过程将每年发生一次。

What you are looking for is a "computed column". 您正在寻找的是“计算列”。 MSSQL and Oracle have these, but I can't find anything about it in MySQL. MSSQL和Oracle都有这些,但是我在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 这样,您可以按照与本文中答案类似的方式在视图内部进行计算基于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;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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