2

我试图连续获得锻炼的总和,然后按总和对所有行进行排序,以获得团队所在的位置,因此以正确的顺序列出。我现在试图弄清楚这一点,我对自己的逻辑有点迷失了。我了解 MYSQL sum 函数,但似乎看不到在这种情况下如何使用它来帮助我。

所以是这样的: 例子

这是我当前的表架构:

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; ?>
4

4 回答 4

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; ?>
于 2012-08-07T21:48:22.320 回答
1

我会将此作为 SQL 的一部分,而不是在 PHP 中。而不是使用Sum()旨在将一列添加到整个分组序列中的使用,只需添加列并为其分配别名“Total”,如下所示:

SELECT week1 + week2 + week3 + weekN Total FROM workouts ORDER BY Total 
于 2012-08-07T21:47:21.540 回答
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 ?>
于 2012-08-07T21:50:07.103 回答
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
于 2012-08-07T21:54:53.410 回答