Jump to content
xisto Community
Sign in to follow this  
Amezis

Ordering Numbers With Mysql?

Recommended Posts

I am making a MySQL-driven list. It can be ordered by name or by a number in the table. However, it orders the numbers like this:

122334567998

But I want the numbers to be ordered like this:
123792398456

How can I do that?

Share this post


Link to post
Share on other sites

I'm n ot an expert in MySQL, but the answer is pretty simple. It orders it that way because of the way the variables in the table are. If, for example you choose varchar(x) it will give you: 1 2 23 3 456 7 9 98 instead of: 1 2 3 7 9 23 98 456That is because it takes in consideration the first letter of the word (number in our case). If they happen to match the difference it made by the second letter. If that also happens to match it check the third ... and so on. And since 1<2<3<4<5<6<7<8<9<0 (in ASCII code) you understand why it orders them in that way (why 3 for example is bigger then 23).If you want to order them as you want: 1 2 3 7 9 23 ... you just have to change the type from varchar(x) or whatever char value are you using to int(x) (int comes from integer). Since int means only numbers it doesn't care about the first letter and takes the hole number and order them. So there is no need to change the code, just change the type.Hope you understand...

Share this post


Link to post
Share on other sites

arre you using an "ORDER by" in the mysql statement? Maybe paste the statement you are using in to a posting and we'll have a better grasp of what you are trying to do.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.