如何为mysql php结果添加增量

时间:2013-06-14 08:37:13

标签: php mysql

我在MySql表中显示8行,我想知道如何在每个结果前面添加数字,例如增量1. - 8.?

这是我的MySql查询行:

$query=mysql_query("SELECT * FROM (SELECT score,pid from score ORDER BY score ASC) As temp GROUP BY temp.pid ORDER BY temp.score ASC LIMIT 8");

2 个答案:

答案 0 :(得分:3)

SELECT @rank := @rank + 1 as row_number, temp.* 
FROM 
(
   SELECT score,pid 
   from score 
   ORDER BY score ASC
) As temp, (select @rank := 0) r 
GROUP BY temp.pid 
ORDER BY temp.score ASC 
LIMIT 8

答案 1 :(得分:0)

在您的 PHP 中,您可以执行以下操作:

$i = 1;
$query=mysql_query("SELECT * FROM (SELECT score,pid from score ORDER BY score ASC) As temp GROUP BY temp.pid ORDER BY temp.score ASC LIMIT 8");
while($row = mysql_fetch_assoc($query))
{
    echo "<tr>
              <td>". $i ."</td>
              <td>". $row['field'] ."</td>
              <td>". $row['another_field'] ."</td>
              <td>". $row['another_field'] ."</td> 
         </tr>";

    $i += 1;
}