Help with ORDER BY (MySQL)
#1

Hey, I'm not the most advanced scripter, actually I've started scripting recently, and I don't know how can I resolve this problem: I want to make it so a player can see his position in the leaderboards, for example, "You are currently in position %d out of %d with %d score!", however I don't know how to get the players position in the leaderboard, everything else works fine. The code is below

Код:
CMD:yourpos(playerid, params[])
{
	new query[128];
	format(query, sizeof(query), "SELECT score, nick FROM playerdata ORDER BY score");
	mysql_query(query);
	mysql_store_result();
	
	new tmpstr[60], p;
	
	
		while(mysql_fetch_row(query))
		{		
			mysql_fetch_field("nick", pInfo[playerid][Nick]);
			mysql_fetch_int("score", pInfo[playerid][pScore]);
			
			p++;
			
			format(tmpstr, sizeof(tmpstr), "You are currently %d out of %d with %d score!", p, ReturnUserCount(), GetPlayerScore(playerid));
			ShowPlayerDialog(playerid, 79, DIALOG_STYLE_MSGBOX, "{10F441}Your score..", tmpstr, "Close", "");
			
		}
	
	return 1;
}
Reply
#2

In order to find the player's position you need to count the number of players that have a score that is higher than the current player's. Such a count query would look somewhat like this:
PHP код:
SELECT COUNT(*) + AS position FROM playerdata WHERE score > ? 
Now the question mark needs to be replaced with the player's score. If you don't know what that is then you need another query to find out.
PHP код:
SELECT score FROM playerdata WHERE id = %
Then you need to combine both queries
PHP код:
SELECT COUNT(*) + AS position FROM playerdata WHERE score > (SELECT score FROM playerdata WHERE id = %d
Also put an index on the score column to speed up searches.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
In order to find the player's position you need to count the number of players that have a score that is higher than the current player's. Such a count query would look somewhat like this:
PHP код:
SELECT COUNT(*) + AS position FROM playerdata WHERE score > ? 
Now the question mark needs to be replaced with the player's score. If you don't know what that is then you need another query to find out.
PHP код:
SELECT score FROM playerdata WHERE id = %
Then you need to combine both queries
PHP код:
SELECT COUNT(*) + AS position FROM playerdata WHERE score > (SELECT score FROM playerdata WHERE id = %d
Also put an index on the score column to speed up searches.
I'm very grateful for you replying, although I don't completely understand it, i would be very thankful if you added your count query to my code I posted above, like I mentioned before, I'm very basic when it comes to these things, and clearly seeing as you're an expert, it shouldn't take you long. You can skip the index part if you want, I can figure out how to do that myself, just want the general command working.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)