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

标签 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

哎呀,我不是故意复制的,但我是不小心复制的。 当它带有“=”符号时(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 (游戏下)

顺便说一句,您在发布查询时如何编码?我点击代码按钮,只有第一行代码被编码。

最佳答案

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

If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another 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`
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_users.userkey。这会产生很多匹配的行。要查看发生了什么,请从查询中删除 LIMIT 子句。

关于php - 如何从 mysql 数据库表中检索最后 10 条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3359445/

相关文章:

php - 无法进行单元测试 : $_SESSION empties before each test is ran

php - MYSQL+无法从PHP连接

php - 如何在单个查询中调整多个 'where' 子句?

mysql > 两侧都有重复项的联接,如何单独连接它们。

mysql - 如果 count(*) 为 null 并且该计数的关联日期返回 0

php - 使用 PHP 将 HTML 转换为 PDF(不是 PDF 到 HTML)

php - 2 个使用 jQuery 或 Ajax 的自动完成/建议输入框,第二个框基于多个项目的第一个选择

php - 使用 fgetcsv() 发出解析数据 - 期望参数为资源

php - 查找最低可用功能

mysql - 错误 1062 (23000) : Duplicate entry though entries are not duplicate