IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Problem On Mysql "order By"


teob
no avatar
Newbie [Level 3]
***
Group: Members
Posts: 43
Joined: 15-November 04
Member No.: 2,292



Post #1 post Jan 15 2005, 04:19 AM
Can someone please help...?
I have a problem with using "ORDER BY" in mysql...
CODE
SELECT * FROM `foo` WHERE b='abc' ORDER BY 'number' ASC

i want to sort the table out by the value in "number" which is a number..

but it came out to be sort by like this way...
1->10->2->20
it only sort out the front digit....

but what i want is it will sort by how great the number is like this...
1->2->3........->10....->20

sorry my english isn't good enough...to describe my problem properly...
hope you can understand and help me on this.....thx
Go to the top of the page
+Quote Post
NilsC
no avatar
Administrator
*********
Group: Members
Posts: 377
Joined: 28-December 04
From: USA
Member No.: 2,992



Post #2 post Jan 15 2005, 05:29 AM
I think you have the syntax wrong. Are you creating a query in mySQL? or calling from a php script?
CODE
SELECT expressions_and_columns FROM table_name[/br][WHERE some_condition_is_true][br][ORDER BY some_column [ASC | DESC]][/br][LIMIT offset, rows]


So yours should be:
CODE
SELECT * FROM foo WHERE b = 'abc' ORDER BY number ASC;
[br]
theoretical questions can have only theoretical answers. You would get better help if you use the table names that you have problem with!

Here is a nice link to a tutorial in your problem:
The select statement.

Nils
Go to the top of the page
+Quote Post
Zenchi
no avatar
Super Member
*********
Group: Members
Posts: 498
Joined: 23-August 04
Member No.: 878



Post #3 post Jan 15 2005, 06:54 AM
This is how mysql sees the way to order those numbers accordingly:

2(null)(null), 20(null), 200, 21(null), 210(null)

Catch my drift? It's like an ftp directory where you see files listed by numbers, and it looks all messed up. It's not. I believe it has to do with the hexidecimal coding, but again, don't quote me on that.
Go to the top of the page
+Quote Post
bjrn
no avatar
Super Member
*********
Group: Members
Posts: 378
Joined: 8-January 05
Member No.: 3,174



Post #4 post Jan 18 2005, 04:43 PM
QUOTE(Zenchi @ Jan 15 2005, 08:54 AM)
This is how mysql sees the way to order those numbers accordingly:

2(null)(null), 20(null), 200, 21(null), 210(null)

Catch my drift? It's like an ftp directory where you see files listed by numbers, and it looks all messed up. It's not. I believe it has to do with the hexidecimal coding, but again, don't quote me on that.
[right][snapback]41324[/snapback][/right]

If number is a string field you have a problem. The other thing could be that you should have "ORDER BY number" instead of "ORDER BY 'number'" (without the single quotes). Although that should give an SQL error if it was wrong... Strange...
Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #5 post Nov 29 2007, 03:00 PM
I had a similar situation with a string field like "SomeString Number", where Number had values like 1, to, 3, ... 55 ...(max to characters) and SomeString had a fixed length = n.

I used a update query like this one:

<code>
update Table set Field = concat(left(Field, n), " 0", right(Field, 1))
where lenth(Field) = n+to;
<code/>

n+to = n (SomeString) + 1 (space) + 1 (for numbers with 1 digit)

So, my Field has values like "SomeString Number", where Number had values like 01, 02, 03, ... 55 ...(exactly to characters) and ORDER BY work well now.

I think this is useful for someone , in this particular situation :)

-gims
Go to the top of the page
+Quote Post
gogoily
no avatar
Member [Level 3]
******
Group: Members
Posts: 99
Joined: 30-October 05
Member No.: 13,571



Post #6 post Dec 6 2007, 05:51 AM
try set the type of "number" to int
I think the type of your "number" is varchar or something.
Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #7 post Aug 1 2009, 06:47 PM

To order the values as they should be, simply use the SQL ABS function, like so:

SELECT * FROM `foo` WHERE b='abc' ORDER BY ABS(price) ASC
-reply by IvanW
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 rob86 191 18th October 2009 - 01:52 AM
Last post by: inverse_bloom
No new   39 the blonde girl 35,977 22nd August 2009 - 10:02 AM
Last post by: ASHISHRANJAN
No New Posts   2 (G)Pradeep Yadav 213 29th August 2009 - 12:04 PM
Last post by: minimcmonkey
No New Posts   1 Ho-oh'sRealm 24 Yesterday, 03:27 PM
Last post by: The Simpleton
No New Posts 3 chalawanonline 6,269 2nd August 2004 - 02:34 PM
Last post by: Spectre
No New Posts   1 groentjuh 5,085 2nd August 2004 - 06:13 PM
Last post by: OpaQue
No New Posts   5 The Simpleton 423 31st August 2009 - 04:21 AM
Last post by: Pankyy
No New Posts   11 velma 1,852 14th August 2009 - 12:14 PM
Last post by: sheepdog
No New Posts 4 hatebreed 9,674 10th August 2004 - 11:10 AM
Last post by: hatebreed
No New Posts   4 nutkitten 1,809 11th October 2008 - 04:33 AM
Last post by: sheepdog
No New Posts   1 mmbg 7,135 13th August 2004 - 06:01 PM
Last post by: odomike
No New Posts   1 SilverBoarder 8,659 17th August 2004 - 04:05 PM
Last post by: odomike
No New Posts 2 ronelm2000 1,053 8th October 2007 - 10:29 AM
Last post by: ronelm2000
No New Posts   7 Raptrex 13,185 27th August 2004 - 11:16 PM
Last post by: Spectre
No New Posts   1 Vacant 5,661 4th March 2009 - 08:13 PM
Last post by: aloKNsh


 



RSS Open Discussion Time is now: 8th November 2009 - 07:19 AM

Web Hosting Powered by ComputingHost.com.