Forum Moderators: coopster

Message Too Old, No Replies

Outing numbers in Order?

How to output numbers from mysql in numerical order?

         

erikcw

4:19 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



Hi All,

I know this is probably a bit of a newbie question, but I can't figure it out.

I have some code that output a list of numbers (10 in this case). The problem is that they are coming out in a 1,10,2,3,4,5,6,7,8,9 fashion instead of 1,2,3,4,5,6,7,8,9,10. What do I do to fix this? Here is my code snipet:

//*****Choose another session.

// generate and execute query
// AND type = 'Handout'
$query = "SELECT * FROM level_session WHERE date <= '$session_current' ORDER BY session ASC";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0) {

?>

<p>Please choose a session to view:<br/>
<form method="post" action="index.php">
<input type="hidden" name="user" value="<? echo $user;?>">
<input type="hidden" name="pw" value="<? echo $pw;?>">
<select name="session_id">
<?

// iterate through resultset
// print title with links to edit and delete scripts
while($row = mysql_fetch_object($result))
{
?>
<option value="<? echo $row->session;?>">Session <? echo $row->session;?></option>
<?
}
?>
</select>
<input type="submit" value="View Session">
</form>
<?
}
// if no records present
// display message
else
{
?>
<p>
<font size="-1">That session could not be located in our database.</font>
<?
}

Thanks for your help!

digitalv

4:28 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can add multiple fields in an ORDER BY statement ... "ORDER BY fieldname,ID" .. is this what you need?

bcc1234

4:38 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the numbers are stored as strings, then it's the natural order.

I don't use MySql, so I'm not sure if it can sort after casting, but if it can then you need to cast your column to an integer type and sort by it.

something like:
... order by my_column_name::int;
or
... order by (int)my_column_name;

Check the MySql docs on how to cast types.

erikcw

4:48 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



If it helps to clarify, the values are stored in mysql as varchar.

Is ading another ORDER command to the sql query going to work for scalability (more sessions may be added later...)

coopster

4:56 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Then that is why your column isn't sorting in the order you expect. As bcc1234 pointed out, that is the natural order. You have a number of options...

You could either ALTER the column to type numeric, or you could use a conversion mechanism when retrieving the data. MySQL automatically converts numbers to strings as necessary, and vice-versa.

CAST()
would be nice, and is preferable, but it is unavailable before MySQL 4.0.2.

You may have to use something else to force a numeric column type, such as something like ABS perhaps:

$query = "SELECT * FROM level_session WHERE date <= '$session_current' ORDER BY ABS(session)";

...or better yet, simply let MySQL do the conversion for you...

$query = "SELECT * FROM level_session WHERE date <= '$session_current' ORDER BY session * 1";

Otherwise, you can just retrieve the columns and use PHP's natcasesort [php.net] function.

Personally, if the session column is going to be numeric, I would ALTER the table and make the column of numeric type. Otherwise, I would use the multiplication CAST-workaround noted above.

digitalv

5:00 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I Microsoft SQL users I don't know if this would work in mySQL, but you could try:

ORDER BY CAST(fieldname as int)

erikcw

5:08 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



Multiplying it by 1 worked like a charm! I didn't change the table because the only number option available was INT & smallint and such. NUM wasn't in the phpMyAdmin list...

Thanks again!

Erik

ergophobe

5:14 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



only number option available was INT & smallint and such.

Aren't the numbers integers (they are in the example, but I don't know if that's true for all cases)?

As long as the largest integer does not exceed 4294967295 you can use an unsigned integer column. For values larger than that, I guess you would need to use a varchar column and then type cast.

Integer cols would take up less disk space and I would guess that searches and indexing would be much faster.

[edit]I was over-generalizing a bit. I should have said that an integer col would take up more space than a single or double digit num in a varchar, the same as a three digit num, and less than anything larger. SMALLINT, however, would take the same space as a single-digit number in a varchar (2 bytes) and would allow values up to 65535.
[/edit]

Tom

[edited by: ergophobe at 5:21 pm (utc) on April 15, 2004]

coopster

5:16 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



INT, SMALLINT...those are numeric column types. Here is a good link for you, erikcw, that describes MySQL column types. You'll want to get a good feel for this MySQL manual, it will come in really handy :)

[dev.mysql.com...]