php mysql 求和并排序

标签 php mysql sorting sum

名为 workout 的 mysql 表具有以下布局:

exercise   |weight|sets|reps|action  | wn
----------------------------------------------
deadlift   |  80  |  6 |  1 | pull   | 32
bb press   |  40  |  3 |  5 | push   | 32
bb rows    |  20  |  2 |  5 | pull   | 31
blahblah   |  blah|  3 |  5 | push   | 31 
blahblah   |  blah|  3 |  5 | squat  | 31
blahblah   |  blah|  3 |  5 | push   | 30
blahblah   |  blah|  3 |  5 | hinge  | 30
blahblah   |  blah|  3 |  5 | carry  | 29

我想将结果排序为每个 Action 的总和*代表,然后按 wn 排序结果并使用 php 显示在表格中。

所以我会为每个 wn 和每个操作的总计排一行,例如:

wn | push | pull | squat | hinge | carry 
----------------------------------------
32 | 80   | 120  |  90   | 180   | 40
31 | 65   | 60   |  80   | 150   | 34
30                          
29

谢谢。

最佳答案

如果我答对了你的问题,你可以这样做:

// $rows are your MySQL fetch_assoc rows
foreach ($rows as $row) {
    if(!isset ($wn_array[$row['wn']][$row['action']]) 
        $wn_array[$row['wn']][$row['action']] = ($row['sets'] * $row['reps']);
    else
        $wn_array[$row['wn']][$row['action']] += ($row['sets'] * $row['reps']); 
}

你会得到一个与你正在寻找的东西相对应的 $wn_array,应该很容易使用。

关于php mysql 求和并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18350165/

相关文章:

node.js - 如何最好地通过 meteor 来 'tail -f' mongo 中的大型集合?

java - 从 PHP 到 Android 的 Bool HTTP 响应

php - 如何在 PHP/Linux 环境中执行一个非常大的 cron

php - 如何在表查询中插入数字格式函数?

mysql - Rails 无效日期错误

mysql - 查找表中 DATETIME 差异的总和?

php - 字符被编码为 �

mysql - 从 textareas 到数据库的文本使得 <br>

ios - swift : Sort array by natural order

python-2.7 - 使用字典列表