Sorting varchar field numerically in MySQL

Try this

SELECT * FROM table_name ORDER BY CAST(field_name as SIGNED INTEGER) ASC

There are a few ways to do this:

  1. Store them as numeric values rather than strings. You've already discounted that as you want to keep strings like 00100 intact with the leading zeros.
  2. Order by the strings cast as numeric. This will work but be aware that it's a performance killer for decent sized databases. Per-row functions don't really scale well.
  3. Add a third column which is the numeric equivalent of the string and index on that. Then use an insert/update trigger to ensure it's set correctly whenever the string column changes.

Since the vast majority of databases are read far more often than written, this third option above amortises the cost of the calculation (done at insert/update) over all selects. Your selects will be blindingly fast since they use the numeric column to order (and no per-row functions).

Your inserts and updates will be slower but that's the price you pay and, to be honest, it's well worth paying.

The use of the trigger maintains the ACID properties of the table since the two columns are kept in step. And it's a well-known idiom that you can usually trade off space for time in most performance optimisations.

We've used this "trick" in many situations, such as storing lower-cased versions of surnames alongside the originals (instead of using something like tolower), lengths of identifying strings to find all users with 7-character ones (instead of using len) and so on.

Keep in mind that it's okay to revert from third normal form for performance provided you understand (and mitigate) the consequences.


Actually i've found something interesting:

SELECT * FROM mytable ORDER BY LPAD(LOWER(mycol), 10,0) DESC

This allows you to order the field like:

1
2
3
10
A
A1
B2
10A
111