Welcome Guest ( Log In | Register)



 
Closed TopicStart new topic
> Ordering Numbers With Mysql?
Amezis
post Mar 28 2006, 01:51 PM
Post #1


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



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:
CODE
1
2
23
3
456
7
9
98


But I want the numbers to be ordered like this:
CODE
1
2
3
7
9
23
98
456


How can I do that?
Go to the top of the page
 
+Quote Post
adriantc
post Mar 28 2006, 02:22 PM
Post #2


Super Member
*********

Group: [HOSTED]
Posts: 498
Joined: 31-January 05
From: Bucharest, Romania
Member No.: 3,516



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 456

That 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...
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 28 2006, 02:27 PM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,753
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
Amezis
post Mar 28 2006, 02:29 PM
Post #4


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Thanks alot adriantc! It works now.
By the way, I can't believe that I set it to varchar unsure.gif

Jlhaslip, I had to simply change the table type to int (I had it on varchar ph34r.gif)

This post has been edited by Amezis: Mar 28 2006, 02:31 PM
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 28 2006, 02:33 PM
Post #5


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,753
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Okay, then I'll close this one down.
Go to the top of the page
 
+Quote Post

Closed TopicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Database With Mysql++(5)
  2. Can You Add Images Into A Mysql Database?(20)
  3. Best Php And Mysql Editor For Noobs(5)
  4. Installing Apache, Mysql, Phpmyadmin Locally(5)
  5. Php And Mysql Programming(2)
  6. Check Referrer To Prevent Linking Yours From Other Sites(7)
  7. Most Efficient Code To Get Prime Numbers(7)
  8. Import From Excel File Into Mysql Database(5)
  9. Mysql, Php, Apache Downloads And Setup Was A Nightmare(9)
  10. Creating Profiles In Php/mysql ?(7)
  11. The Best Free Php Mysql Blog(5)
  12. Testimonials(81)
  13. Index In A Mysql Database(3)
  14. Mysql In Visual Basic(5)
  15. Mysql(7)
  1. Getting Started With Mysql(2)
  2. I Need Help(7)
  3. Mysql-essential-5.0.51 Installion Problem(1)
  4. I Keep Seeing The Same Numbers Everywhere(11)
  5. Simple User System(19)
  6. Ch Customer. - Need Help W/ Php/mysql Settings. System() Passthru() Mysqldump(1)
  7. Mysql Query Problems(6)
  8. Best Sites For Learning Php-mysql(4)
  9. Tools Needed!(6)
  10. Mysql Database Space(3)
  11. What Is Mysql Host Address?(6)
  12. Increment A Mysql Column(6)
  13. Delphi 2005 + Mysql(2)
  14. Vb Select Mysql Database(2)
  15. Qupis : Free Hosting With Php, Mysql, Cpanel. (one Line Text Ad At Bottom)(40)


 



- Lo-Fi Version Time is now: 16th May 2008 - 02:57 PM