MySQL : ORDER BY numbers

Sat, May 9, 2009

MySQL

In this article I will show you how to order MySQL records by numeric values (numbers, integers & floats) from the smallest to the largest number or the other way around using a MySQL ORDER BY query.

A MySQL ORDER BY query with a given field name will order/sort the MySQL records alphabetically for the specified field name. If the field specified contains numbers, they will still be sorted alphabetically. Say we have 5 (five) records in the database table with the following values in the “price” field/column.

  • 19.99
  • 125
  • 5.99
  • 199
  • 300

Lets execute a normal ORDER BY MySQL on the table and attempt to order/sort the records by the “price” field in an ascending manner. See the query below.

SELECT * FROM `table` ORDER BY `price` ASC;

You might expect the query above to order records by price from the record with the smallest/lowest price in an ascending manner to the record with the biggest/highest price but this won’t necessarily happen. The records will be sorted with the “price” field in the following order.

  • 125
  • 199
  • 19.99
  • 300
  • 5.99

As you can see the numbers are being ordered alphabetically and not as expected from the smallest/lowest number to the biggest/highest number. You can get the correct order as expected though by using the MySQL ABS modifier. It tells MySQL to process the field as an absolute numeric value. See the example below with the MySQL ABS modifier.

SELECT * FROM `table` ORDER BY ABS(`price`) ASC;

The result of using MySQL ABS are the records ordered with the “price” field from the lowest price to the highest price as expected. This can be used with any MySQL field/column which holds numeric values. You can use this on INT, BIGINT, FLOAT and other MySQL field types.

  • 5.99
  • 19.99
  • 125
  • 199
  • 300
, , , , , , , , , , , , , ,
slideshow-widejpg

This post was written by:

- who has written 71 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

24 Comments For This Post

  1. IFO Says:

    Thanks for the tutorial!

  2. Berg Says:

    Thanks for the tips

  3. Natali Ardianto Says:

    You should try this:
    SELECT * FROM `table` ORDER BY (`price` + 0) ASC;

    Assuming that the data type is not numeric.

  4. Schmuck Says:

    Hey, thank you. I have searched for this a long time!

  5. Berg Says:

    Hey thanks for the tip. Will try it out this weekend.

  6. o Says:

    What? Why the hell would you store prices in a text column? That’s horrendously inefficient and foolish. Use the correct data type for the column in the first place.

  7. Web design Group Says:

    How do I connect to two MySQL databases at two different servers from a single php page?

  8. hausundgarten Says:

    Many thanks for that My SQL Tip. I hope you will post again in the future.

  9. غرائب Says:

    How do I connect to two MySQL databases at two different servers from a single php page?

  10. MusicIsMyLife Says:

    There is nothing special in sorting by numbers. You can sort in same way by strings, dates or even enums.
    Have a nice day )

  11. Adnan Says:

    I come at this page while Google.. really helped a lot this tut.

  12. bisnis internets Says:

    thanks very nice article

  13. Melongal Says:

    Thanks for the tips! that was just what I needed! :)

  14. andy93 Says:

    In this article I will show you how to order MySQL records by numeric values (numbers, integers

  15. fictus Says:

    Wow! This even works on text fields that include numbers and text characters together!

    For example, my field was able to sort numbers even when I had values as such:

    230-Medium
    440-High
    110-Low

    Thanks for the example!

  16. Carlos Granier-Phelps Says:

    Thanks! Saved me a ton of research, trial and error – trying to sort numerically on a longtext field. Worked like a charm.

  17. biotechnology student Says:

    pls tell me how to concatinate two number fields in mysql.

  18. Bruno Thomasi Says:

    thx man from Brasil!

  19. bobby Says:

    thx for code

  20. nuntius Says:

    Thanks for the example.

    Another way is to use the +0 option (although I understand it is a bit slower), but it sometimes works when ABS() wont. I had an problem where we had been numbering our issues as volume.issue_number.

    For example. 1.1 is the first issue of the first volume and 1.10 is the tenth issue of the first volume. The proper way would have been to get them to use .01, but let’s face it… we don’t always thing that way in advance, so I was in a bind! I tried ABS and it didn’t work in this situation for some reason, so I turned to +0.

    What I discovered after a lot of research is that you can do something fancy by using the +0 option combined with SUBSTRING_INDEX and ORDER BY to split a field during the query.

    SELECT DISTINCT issue FROM tbl_pages
    WHERE isActive=1
    ORDER BY
    (SUBSTRING_INDEX(issue, ‘.’, 1)+0),
    (SUBSTRING_INDEX(issue, ‘.’, -1)+0)

    This query first orders the issues numerically by the volume number in the field and then orders the issue numerically by issue number. SUBSTRING_INDEX is recognizing the period as a delimiter and
    breaking up the column, in order to sort correctly.

    Hope this helps someone.

  21. Zno Says:

    hi all
    my problem is similar to fictus but unfortunately the “first” number can be 1,2 or 3 digit(s).
    how can i sort in right way?

    for example:
    1mm N52 NEODYM
    10mm N52 NEODYM
    100mm N52 NEODYM
    2mm N52 NEODYM
    3mm N52 NEODYM

    what i need:
    1mm N52 NEODYM
    2mm N52 NEODYM
    3mm N52 NEODYM
    10mm N52 NEODYM
    100mm N52 NEODYM

  22. muhammed sekertekin Says:

    Thank you very much this helps me a lot. i almost give up with mysql query until i saw this article ….muhammed sekertekin

  23. Oslo Hudlege Says:

    thank you so much for solving my issue…

  24. Narong Says:

    Thanks for the tutorial!

Leave a Reply