Jump to content

Numbering issues with mysql


SCook

Recommended Posts

Hi all,

 

I know this is primarily a mysql question, but I think it has other uses also.  If I query my db and ask for the max(date) for example, everything works unless the highest date is January.  This is due to the fact that a computer wants to order numbers like this: 1, 11, 12, 2, 3, 4, ......

 

Is there s way around this failure?  I want to make sure I always get the furtherest date.  Dates are in 1/10/2008 format.  Thanks

Link to comment
Share on other sites

If it's varchar, it will be sorted the same way text would be....

 

If they were yyyy/mm/dd you could probably wrap them in a DATE() function to sort them....  Hrmmm....

 

 

The reason it won't work is as follows:

 

1/1/08 is obviously before 12/1/08, but 2 is before / in alphabetical sorting, so it wins.

 

Edit: actually I'm not sure which order MySQL considers characters in, but I know for sure what's causing the problem in the first post:

 

When things are sorted alphabetically, first the first letter is sorted....

 

Example of original data:

aa

ba

ab

bc

ac

bb

 

First letter sort:

 

aa

ab

bc

bb

 

Second letter sort:

 

aa

ab

bb

bc

 

So with numbers it does the same thing:

 

Original:

1

11

2

27

24

3

 

First number:

 

1

11

2

27

24

3

 

Second number:

 

1

11

2

3

24

27

Link to comment
Share on other sites

Then it's by design.  Change it to a one of the Number formats instead.

 

It is a varchar().  Thinking about it, I could've saved the date in a different format, but it is what it is.  I will try the order by, but I don't think that will help.  Simply because the way computers want to order things.

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.