Page 1 of 1

ORDER BY

Posted: Fri Jul 01, 2005 7:15 am
by mickd
hi, i got the following code but the order by claus isnt ordering it...

Code: Select all

$result = mysql_query("SELECT cost, username FROM table ORDER BY cost DESC LIMIT 4") or die(mysql_error());
echo '<table border="1" align="center">';
echo '<tr><td width="500" height="15" colspan="2" align="center"><strong>Result</strong></td></tr>';
echo '<tr><td width="400" height="15" align="left">Username:</td><td width="100" height="15" align="left">Cost:</td></tr>';
while($row = mysql_fetch_array($result))
{
echo '<tr><td width="400" height="15">'; echo $row[1];
echo '</td><td width="100" height="15">'; echo $row[0];
echo '</td></tr>';
}
what this returns is:
Username: Cost:
hagrg 97952
sdjhe 4779
qfhdf 300000
rggrg 130059

as you can see the cost isnt in decreasing order like it should be.. (300000, 130059, 97952, 4779)

any help appriciated...

Posted: Fri Jul 01, 2005 2:38 pm
by kendall
yo,

is the cost column a INT typ or VARCHAR type column. Should be set to INTEGER/ INT type column where it treats the data as a numeric type for the situation youre in.

Posted: Fri Jul 01, 2005 7:32 pm
by mickd
hi, thanks for your reply, yes the cost column was set to varchar but the value was still numbers, ill try what you said and see if it works :)

thanks again.

EDIT: it works :) thanks.

EDIT2: btw i came accross another wierd problem... if the column is set to int(255) for some reason the maximum number it allows is 2147483647. when i try to set it to 1000000000000 to test it, it automatically puts it back down to 2147483647. any help appriciated :)

Posted: Sat Jul 02, 2005 3:42 am
by timvw
Happy reading:

http://dev.mysql.com/doc/mysql/en/numeric-types.html


Btw, there are lot of people who think that mysql should throw an error if you try to insert invalid data instead of modifying your data...

Posted: Sat Jul 02, 2005 8:57 am
by mickd
thanks works now :)