ORDER BY

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

ORDER BY

Post 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...
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

thanks works now :)
Post Reply