SparkAndShine
SparkAndShine

Reputation: 17997

How to ORDER BY a field consisting of digits following by alphabets in MySQL?

How to ORDER BY a field consisting of digits following by alphabets, such as 10s, in MySQL? The desired order is:

For instance,

# Original data:
1
10
105
109
10s

# Desired results
1
10
10s
105
109

Upvotes: 1

Views: 51

Answers (2)

raina77ow
raina77ow

Reputation: 106365

One possible approach:

ORDER BY CAST(alphanum AS UNSIGNED), alphanum

... or, slightly more concise:

ORDER BY alphanum + 0, alphanum

Demo.

Upvotes: 1

Hexana
Hexana

Reputation: 1135

Try this:

SELECT values FROM Table ORDER BY CAST(values AS UNSIGNED), values;

Upvotes: 2

Related Questions