我有一個奇怪的問題。當我運行這個腳本時,我從數據庫中獲得了10條記錄,但它們都完全一樣。我不知道我在做什麼錯誤或如何解決它。請幫幫我。如何從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)我得到空的查詢,以便didnt解決它:(
我希望這個查詢是比較容易理解:)
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 (在遊戲中)
順便說一句,當你將它張貼你如何編寫查詢?我點擊了代碼按鈕,只有代碼的第一行被編碼。
請格式化您的查詢,使其可讀性,以便我們可以幫助您 – Gopi 2010-07-29 04:22:56