0

如何查询和显示每条记录上的学生钱总和的记录?

 Student ID   Student Name   Student Money 
 ---------    -----------     --------------
   1           John            100
   2           Jenny           200
   3           Ben             100
   4           Andy            200
   5           Lynna           100

上面是我的表,我想以这种格式检索记录:

 Student ID   Student Name   Student Money     totalCounting 
 ---------    -----------     --------------   ---------
   1           John               100           100
   2           Jenny              200           300
   3           Ben                100           400
   4           Andy               200           600
   5           Lynna              100           700
4

2 回答 2

0
Select s.*, (SELECT SUM(s2.`Money`) from students as s2 WHEre s2.`ID` = s.`ID`) as 'totalCounting' from students AS s;

像这样的东西。

于 2012-04-11T12:49:18.540 回答
-1

在 php 中,我希望在使用 foreach 循环的查询构建的数组中构建一个额外的键。我的 SQL 不够强大,无法将其构建到查询中,所以......

$total = 0;
foreach ($queryResults as $key=>$row){
    $total = $total + $row['student_money'];
    $queryResults[$key]['total_money'] = $total;
}

然后使用带有新列的 $queryResults 构建表。

于 2012-04-11T13:13:50.227 回答