-1

フィールド:値を含む学生テーブルがあります

firstname : John
lastname : Doe
english-grd : 87
math-grd : 80
science-grd : 85
total-grade : 0

私の質問は、「-grd」のみでフィールドを取得し、合計する方法です。

4

3 に答える 3

2

クエリ:

Select english-grd,math-grd,science-grd,(english-grd+math-grd+science-grd) as tot from table
于 2012-10-31T06:29:55.497 に答える
0
update tableName set `total-grade` = (
`english-grd` + `math-grd` + `science-grd`
)
于 2012-10-31T06:36:49.777 に答える
0
// this example just update one row
$student_id = 1; // example only
$res=mysql_query("SELECT * FROM Student where student_id='" . $student_id . "'");
$field_count = mysql_num_fields($res); //count field numbers
$total_grades = 0;
for ($i = 0; $i < $field_count; $i++)
{   
    $field_name=mysql_field_name($res, $i);
    if (substr($field_name,-4) == '-grd')
    {
        $total_grades = $total_grades + mysql_result($res,0,$i); // 0 refer to first row ...there is only one row from $res...$i is offset of the column
    }
}
mysql_query("update Student SET total-grade='" . $total_grades . "' where student_id='" . $student_id . "'");
于 2012-10-31T08:46:57.567 に答える