Jump to content

Order by 2 numbers?


arbitter

Recommended Posts

Hello there,

 

I have a table with dates in one column. Now, these dates are eg '8', and another one could be '24'. When I order it using

$row=mysql_fetch_array(mysql_query("SELECT * FROM activities WHERE month='9' ORDER BY day ASC"),MYSQL_ASSOC);

, the 24 get's displayed before the 8. This is logic, but nevertheless, how can I change this?

 

Because when I use this method to order things by id, it does work corectly.

Link to comment
Share on other sites

The BEST thing to do would be to store dates in the appropriate DATE or DATETIME field type. Lacking that, either change the fields to TINYINT as suggested above, or typecast the field that is ORDERed BY.

 

"SELECT * FROM activities WHERE month='9' ORDER BY CAST( day AS UNSIGNED INT) ASC"

Link to comment
Share on other sites

Hm probably, but I find i tannoying to have an extra field because then you need to keep that in mind... But I guess that's the only solution? Adding a begindate and an enddate field seperately?

 

Depends on what all you need it to do - but yes, that's likely what you'll want to do.  You can have the enddate field set to NULL if there is no range.  I did a calendar app once that had events on a day or a range of days, and did just that - if the end date was null, it was on a single date, otherwise it spanned from the start date to the end date.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.