我试图连续获得锻炼总数,然后按总和排序所有行,以获得团队所在的位置,从而以正确的顺序列出。我现在正在尝试解决这个问题。我理解MYSQL求和函数但似乎无法在这种情况下看到我如何用它来帮助我。
这样的事情:
这是我当前的表架构:
CREATE TABLE workouts
(
team_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VarChar(255) NOT NULL,
team_name VarChar(50) NOT NULL,
week1 INT NOT NULL,
week2 INT NOT NULL,
week3 INT NOT NULL,
week4 INT NOT NULL,
week5 INT NOT NULL,
week6 INT NOT NULL,
week7 INT NOT NULL,
week8 INT NOT NULL,
week9 INT NOT NULL,
week10 INT NOT NULL,
week11 INT NOT NULL,
week12 INT NOT NULL
) engine=innodb;
到目前为止,这是我的展示:
<?php
$count = 0;
$statement = $db->query('SELECT * FROM workouts');
foreach($statement as $row):
?>
<tr>
<td><?php $count++; ?></td>
<td><?php $row['team_name']; ?></td>
<td><?php $row['week1']; ?></td>
<td><?php $row['week2']; ?></td>
<td><?php $row['week3']; ?></td>
<td><?php $row['week4']; ?></td>
<td><?php $row['week5']; ?></td>
<td><?php $row['week6']; ?></td>
<td><?php $row['week7']; ?></td>
<td><?php $row['week8']; ?></td>
<td><?php $row['week9']; ?></td>
<td><?php $row['week10']; ?></td>
<td><?php $row['week11']; ?></td>
<td><?php $row['week12']; ?></td>
<td><?php ?></td>
</tr>
<?php endforeach; ?>
答案 0 :(得分:2)
根据您已经有的总结行的最简单答案是:
<?php
$count = 0;
$statement = $db->query('SELECT * FROM workouts');
foreach($statement as $row):
?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['week1']; ?></td>
<td><?php echo $row['week2']; ?></td>
<td><?php echo $row['week3']; ?></td>
<td><?php echo $row['week4']; ?></td>
<td><?php echo $row['week5']; ?></td>
<td><?php echo $row['week6']; ?></td>
<td><?php echo $row['week7']; ?></td>
<td><?php echo $row['week8']; ?></td>
<td><?php echo $row['week9']; ?></td>
<td><?php echo $row['week10']; ?></td>
<td><?php echo $row['week11']; ?></td>
<td><?php echo $row['week12']; ?></td>
<td><?php echo $row['week1'] + $row['week2'] + $row['week3'] + $row['week4'] + $row['week5'] + $row['week6'] + $row['week7'] + $row['week8'] + $row['week9'] + $row['week10'] + $row['week11'] + $row['week12']; ?></td>
</tr>
<?php endforeach; ?>
但是为了使用这个值来控制行的显示顺序,你需要让MySQL为你做这项工作。
你想要做的是这样的事情:
<?php
$count = 0;
$statement = $db->query('
SELECT *, week1 + week2 + week3 + week4 + week5 + week6 + week7 + week8 + week9 + week10 + week11 + week12 AS Total
FROM workouts
ORDER BY Total
');
foreach ($statement as $row):
?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $row['team_name']; ?></td>
<td><?php echo $row['week1']; ?></td>
<td><?php echo $row['week2']; ?></td>
<td><?php echo $row['week3']; ?></td>
<td><?php echo $row['week4']; ?></td>
<td><?php echo $row['week5']; ?></td>
<td><?php echo $row['week6']; ?></td>
<td><?php echo $row['week7']; ?></td>
<td><?php echo $row['week8']; ?></td>
<td><?php echo $row['week9']; ?></td>
<td><?php echo $row['week10']; ?></td>
<td><?php echo $row['week11']; ?></td>
<td><?php echo $row['week12']; ?></td>
<td><?php echo $row['Total']; ?></td>
</tr>
<?php endforeach; ?>
答案 1 :(得分:1)
我会将其作为SQL的一部分,而不是PHP。而不是使用Sum()
,它被设计为在整个分组序列中添加一列,只需添加列并为它们分配别名'Total',如下所示:
SELECT week1 + week2 + week3 + weekN Total FROM workouts ORDER BY Total
答案 2 :(得分:1)
这是我的解决方案。
我不是仅仅将数据库值作为处理它们进行回显,而是将它们转储到一个关联数组中,并将总数作为关键字。然后你可以使用ksort对它们进行排序。完成后,您可以使用foreach循环打印出数组。
我没有输入完整的数组函数,但它应该给你一个想法。
<?php
$count = 0;
$statement = $db->query('SELECT * FROM workouts');
$workouts = array();
while ($row = mysql_fetch_array($statement)) {
$total = 0;
for($i = 0;$i < 13; $i++)
$total = $total + $row['week'.$i];
//Build the array containing each teams workouts
$workouts[$total] = array('team_name' => $row['team_name'],'week1' => $row['week1']);
}
ksort($workouts);
$rowcount = 0;
foreach ($workouts as $total => $team): ?>
<tr>
<td><?php echo $rowcount++; ?></td>
<td><?php echo $team['team_name']; ?></td>
<td><?php echo $team['week1']; ?></td>
<td><?php echo $team['week2']; ?></td>
<td><?php echo $team['week3']; ?></td>
<td><?php echo $team['week4']; ?></td>
<td><?php echo $team['week5']; ?></td>
<td><?php echo $team['week6']; ?></td>
<td><?php echo $team['week7']; ?></td>
<td><?php echo $team['week8']; ?></td>
<td><?php echo $team['week9']; ?></td>
<td><?php echo $team['week10']; ?></td>
<td><?php echo $team['week11']; ?></td>
<td><?php echo $team['week12']; ?></td>
<td><?php echo $total ?></td>
</tr>
<?php endforeach ?>
答案 3 :(得分:1)
如果我理解正确,您需要团队按顺序与每个团队中的用户(可能按照自己的顺序)。
要获得团队订单,您需要加入。
with wo as (select wo.*,
(week1 + week2 + . . . weekn) as weektotal
from workouts
)
select wo.*
from wo join
(select team_id, sum(weektotal) as weektotal
from wo
group by team_id
) wot
on wo.team_id = wot.team_id
order by wot.weektotal desc, wot.team_id, wo.weektotal desc