0

我需要为我正在制作的测验网站设计一个排行榜。我将用户分数存储在名为“user_record”的数据库表中,该表具有以下结构:

'user_id'  - varchar(254) -which holds the user id for a particular score
'score'  - int(2) -which holds the score
'time'  - timesatamp -which has the CURRENT_TIMESTAMP
'date'  - date -which has the CURDATE()

现在我需要对当前日期的每个 user_id 的值求和,并按降序显示前 5 个。我有以下代码,但它似乎不起作用。有什么帮助吗?

<table>
    <tr>
    <th>User Id</th>
    <th>Score</th>
    </tr>
    <?php
    include "connection.php";
    $w=mysql_query("SELECT user_id,SUM(Score) from score WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5");
    $b=$row=mysql_fetch_array($w);
    while($b)
    {
    $user_id=$row['user_id'];
    $score=$row['score'];
    ?>
    <tr>
    <td><?php echo $user_id;?></td>
    <td><?php echo $score; ?></td>
    </tr>
    <?php
    }
    ?>  
</table>
4

2 回答 2

1

替换这个

  $w=mysql_query("SELECT user_id,SUM(Score) from score WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5"); 
  $b=$row=mysql_fetch_array($w);
  while($b) 

经过

 $w=mysql_query("SELECT user_id,SUM(Score) as score from user_record WHERE date=CURDATE() GROUP BY user_id ORDER BY score DESC limit 5");
  while( $row=mysql_fetch_array($w) ) 
于 2013-10-01T19:43:37.123 回答
0

正确的查询是

SELECT user_id, SUM(score) from user_record WHERE date=CURDATE() GROUP BY user_id ORDER BY SUM(score) DESC limit 5
于 2013-10-01T19:47:48.810 回答