我需要为我正在制作的测验网站设计一个排行榜。我将用户分数存储在名为“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>
答案 0 :(得分: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) )
答案 1 :(得分:0)
正确的查询是
SELECT user_id, SUM(score) from user_record WHERE date=CURDATE() GROUP BY user_id ORDER BY SUM(score) DESC limit 5