Click to See Complete Forum and Search --> : Descending Order
khayman2001
06-26-2003, 10:37 PM
Ok, I'm trying to make a ranking page, where it lists the top scorers. I have a table worked out, but I'm having trouble figuring out how to order the users. This is the select I'm using: SELECT username, nickname, score FROM users WHERE nickname != "" ORDER BY people DESC but that only orders descending by the first digit, and I have some people in the thousands, and some only in the fifties, so I get an order rank like 9054, 8235, 603, 53, 407, 449, 22.
I have no clue how to do this further, so any help would be appreciated.
Try this SQL statement:
"SELECT username, nickname, score FROM users ORDER BY score DESC";
khayman2001
06-26-2003, 10:52 PM
I had the WHERE nickname != "" because in the game I'm writing, people create a character after they recieve the password generated in registration, and if they hadn't created a character yet, (i.e., the nickname of their character would not be blank) then I didn't want them displayed in the rankings.
Ok, so just add it in:
"SELECT username, nickname, score FROM users WHERE nickname != "" ORDER BY score DESC";
khayman2001
06-27-2003, 06:45 PM
That's what I tried, but it just takes the first digit and sorts by descending that way, not taking into account the number of digits in each number.
I don't know SQL well enough to tell you how to do it in that, but you could just read the records into an array and use PHP to sort them, like this:
<?PHP
$array = array("103","570","315","264","489");
rsort($array); #sort array from highest to lowest (to sort from lowest to highest, remove the r in rsort -- sort($array); )
foreach ($array as $value) {
echo $value."<br>";
}
?>
Originally posted by khayman2001
That's what I tried, but it just takes the first digit and sorts by descending that way,
not taking into account the number of digits in each number.
if the field is a text/char type field, the sort is performed in a left to right sequence. the "numbers" in a text field are not numeric. Non-numeric "numbers" are not used in arithmetic opertions ( add, substract, multiply and divide ), for example, your telephone "number" or social security "number" or account "number."
if the field is a numberic type field, the sort is performed on the basis of the numerical value of the field.
khayman2001
07-02-2003, 09:00 PM
Thank you, so much. That was my problem, I had the type set on the field to varchar instead of int. Anyway, thanks a bunch!
:D