Tiit Remmel
Tiit Remmel

Reputation: 230

Mysql how to ORDER BY?

I need to order following rows:
10a
10b
11c
5a
5b
5c
9c

and the result should be:
5a
5b
5c
9c
10a
10b
11c

now my query looks like this:

SELECT klass,id FROM klassid WHERE klass!='' ORDER BY klass ASC

Is it possible?

thanks in advance

Upvotes: 1

Views: 67

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562991

To ensure numerical ordering, coerce the value to an integer. An easy way to do this is to put it in a numeric expression context.

Then to resolve ties, order by the original string value.

SELECT klass,id FROM klassid WHERE klass!='' 
ORDER BY klass+0 ASC, klass ASC

Upvotes: 3

Related Questions