php mysql求和和和排序方式
名为“训练”的mysql表具有以下布局:php mysql求和和和排序方式,php,mysql,sorting,sum,Php,Mysql,Sorting,Sum,名为“训练”的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 |
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
我想将结果排序为每个操作的总和集*reps,然后按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\u数组,它与您要查找的内容相对应,应该很容易使用。如果所有操作都是预先知道的,您可以进行条件求和 选择wn, SUMCASE WHEN action='push'然后设置*reps END push, SUMCASE WHEN action='pull'然后设置*reps END pull, SUMCASE WHEN action='shub'然后设置*reps END shub, SUMCASE当动作=‘铰链’然后设置*重复结束铰链, SUMCASE当action='carry'然后设置*reps END carry 从训练中 按wn描述分组 样本输出: | WN | PUSH | PULL | SQUAT | HINGE | CARRY | --------------------------------------------------- | 32 | 15 | 6 | (null) | (null) | (null) | | 31 | 15 | 10 | 15 | (null) | (null) | | 30 | 15 | (null) | (null) | 15 | (null) | | 29 | (null) | (null) | (null) | (null) | 15 |
这是演示@chocman它有帮助吗?你的问题需要更多的帮助吗?