dongwei3336 2013-10-26 05:14
浏览 802
已采纳

Mysql sum函数结果显示在每一行

I've following db structure.

id      email     lat      long      point      balance    date
1       33        1.00     2.00      0          empty      date 
2       34        8.00     3.00      -1         empty      date
3       33        7.00     4.00      2          empty      date
4       33        6.00     5.00      0          empty      date
5       33        6.33     5.43      -1         empty      date

so i want to show all record which email id is 33 but it's must be show the balance in every row for example.

In first row it's balance is 0
second row it's balance is   2
third row it's balance is    2
four row it's balance is     1 

so my php code is look like this but can't get correct balance:

echo "<table width='100%' cellpadding='5' cellspacing='0' border='1'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='100'><b>Date</b></td>";    
echo "<td class='tdhead' valign='top' width='100'><b>Lattitude</b></td>";
echo "<td class='tdhead' valign='top' width='50'><b>Longitude</b>
</td>";                     
echo "<td class='tdhead' valign='top' width='50'><b>Point</b>
</td>";                     
echo "<td class='tdhead' valign='top' width='50'><b>Balance</b>
</td>";                     
echo "</tr>";

while($res =  mysql_fetch_array($park_history))
{
    $lat = $res['lat'];
    $long = $res['long'];
    $point =  $res['point'];
    $date = $res['date'];           
    $balance = 0;

$sum = mysql_query("SELECT SUM(point) AS points FROM balance WHERE email = 
'".$_SESSION['SESS_ID']."'");
    $sum_res = mysql_fetch_array($sum);
    $sum = $sum_res['points'];

    echo "<tr>";                
            echo "<td class='tdhead2' valign='top'>$date</td>";
            echo "<td class='tdhead2' valign='top'>$lat</td>";
            echo "<td class='tdhead2' valign='top'>$long</td>";
            echo "<td class='tdhead2' valign='top'>$point</td>";
            echo "<td class='tdhead2'    
valign='top'>$sum</td>";                        
    echo "</tr>";
}

I believe it can be done using mysql sum function. Can you plz give me solutions or suggestions. Thank You.

  • 写回答

2条回答 默认 最新

  • douyan6742 2013-10-26 05:37
    关注

    The MySQL sum function won't do what you want it to - but it doesn't have to - there's a much easier way to acomplish your task using the result you already fetched.

    Since you already have the $point for the row you're operating on, simply add that to a counter and proceed from there. As it is, you're making a superfluous db call every row.

    Use:

    $sum = 0;
    
    while ( $res = mysql_fetch_array($park_history) ) {
    
        /* yada yada */
        $point =  $res['point'];
        $sum += $point;
    
        echo /* your table here */
    
    }
    

    You can completely drop the lines:

    $sum = mysql_query( ... );
    $sum_res = mysql_fetch_array($sum);
    $sum = $sum_res['points'];
    

    $total will hold the running points tally as you described and won't query your db every loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。