如何从mysql数据库表中检索最后10条记录?

时间:2010-07-29 04:17:51

标签: php mysql

我有一个奇怪的问题。当我运行这个脚本时,我从数据库中获得了10条记录,但它们完全相同。我不知道我做错了什么或如何解决它。请帮帮我。

我有AMCMS_highscores,AMCMS_users,AMCMS_games这些表我想查看AMCMS_highscores表内部,获取最后10条记录,但仅限于现场gameid为1997的地方。任何帮助表示赞赏。

$data = query("SELECT `AMCMS_highscores`.`primkey` , `AMCMS_highscores`.`gameid` , `AMCMS_highscores`.`score` , `AMCMS_users`.`username` , `AMCMS_highscores`.`status` , `AMCMS_highscores`.`userkey` , `AMCMS_games`.`primkey` , `AMCMS_games`.`gamename` FROM `AMCMS_highscores` , `AMCMS_games` , `AMCMS_users` WHERE `AMCMS_highscores`.`gameid` = '$gameid' AND `AMCMS_highscores`.`userkey` != `AMCMS_users`.`userkey` AND `AMCMS_highscores`.`gameid` = `AMCMS_games`.`primkey` AND `AMCMS_highscores`.`status`= 'approved' ORDER by `AMCMS_highscores`.`primkey` DESC LIMIT 0, 10");

Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data )) {
    Print "<tr>";
    Print "<th>Score:</th> <td>".$info['score'] . "</td> ";
    Print "<th>ID:</th> <td>".$info['userkey'] . " </td></tr>"; }
Print "</table>";

以下是查询的格式化版本:

SELECT
    `AMCMS_highscores`.`primkey` , `AMCMS_highscores`.`gameid` ,
    `AMCMS_highscores`.`score` , `AMCMS_users`.`username` ,
    `AMCMS_highscores`.`status` , `AMCMS_highscores`.`userkey` ,
    `AMCMS_games`.`primkey` , `AMCMS_games`.`gamename`
FROM `AMCMS_highscores` , `AMCMS_games` , `AMCMS_users`
WHERE `AMCMS_highscores`.`gameid` = '$gameid'
AND `AMCMS_highscores`.`userkey` != `AMCMS_users`.`userkey`
AND `AMCMS_highscores`.`gameid` = `AMCMS_games`.`primkey`
AND `AMCMS_highscores`.`status`= 'approved'
ORDER by `AMCMS_highscores`.`primkey` DESC
LIMIT 0, 10

OOPS,我不是故意复制,但我偶然做了。 当它带有“=”符号(highscores.userkey = users.userkey)时,我得到空查询,以便没有修复它:(

我希望这个查询更容易理解:)

SELECT highscores.primkey, highscores.gameid, highscores.score, users.username,
highscores.status, highscores.userkey, games.primkey, games.gamename 

FROM AMCMS_highscores AS highscores, AMCMS_games as games, AMCMS_users as users

WHERE highscores.gameid = '$gameid' AND
      highscores.status = 'approved'

ORDER by highscores.primkey DESC LIMIT 0, 10

结果如下:

http://www.gamesorbiter.com/FB_app/play.php?gameid=1997 (根据比赛)

顺便问一下,如何在发布时对您的查询进行编码?我点击了代码按钮,只有代码的第一行被编码。

2 个答案:

答案 0 :(得分:2)

如果您尝试从AMCMS_users中选择AMCMS_highscores中没有匹配行的记录,那么您需要执行LEFT JOIN,并在连接中测试NULL值表。以下是JOIN Syntax的摘录:

  

如果没有匹配的行   ONUSING中的右表   部分在LEFT JOIN,一行包含所有   设置为NULL的列用于   右表。你可以用这个事实来   查找表中没有的行   另一个表中的对应部分:

因此,您的查询看起来更像是:

SELECT
    `AMCMS_highscores`.`primkey` , `AMCMS_highscores`.`gameid` ,
    `AMCMS_highscores`.`score` , `AMCMS_users`.`username` ,
    `AMCMS_highscores`.`status` , `AMCMS_highscores`.`userkey` ,
    `AMCMS_games`.`primkey` , `AMCMS_games`.`gamename`
FROM `AMCMS_highscores`
LEFT JOIN `AMCMS_users`
    ON `AMCMS_highscores`.`userkey` = `AMCMS_users`.`userkey`
JOIN `AMCMS_games`
    ON `AMCMS_highscores`.`gameid` = `AMCMS_games`.`primkey`
WHERE `AMCMS_highscores`.`gameid` = '$gameid'
AND `AMCMS_highscores`.`status`= 'approved'
AND `AMCMS_users`.`userkey` = NULL
ORDER by `AMCMS_highscores`.`primkey` DESC
LIMIT 0, 10;

暂时忽略其他条件,您的查询会查看AMCMS_highscores中的每一行,并将其加入AMCMS_highscores AMCMS_highscores中的每一行。userkey是不等于AMCMS_usersuserkey。这将产生许多匹配的行。要查看发生的情况,请从查询中删除LIMIT子句。

答案 1 :(得分:1)

别名和更好的格式相同:

SELECT highscores.primkey, highscores.gameid, highscores.score, users.username,
highscores.status, highscores.userkey, games.primkey, games.gamename 

FROM AMCMS_highscores AS highscores, AMCMS_games as games, AMCMS_users as users

WHERE highscores.gameid = '$gameid' AND
      highscores.userkey != users.userkey AND
      highscores.gameid = games.primkey AND
      highscores.status = 'approved'

ORDER by highscores.primkey DESC LIMIT 0, 10

你没有说出表格方案是什么,但我的猜测是问题出在WHERE部分,更具体地说是highscores.userkey != users.userkey。它仅接受具有与highscores表中相同的用户密钥的用户。尝试将!=更改为=