Wednesday, 20 June 2012

MySQL ordering by a specific fields values...

I often get requests after completing reports for all sorts of interesting ways to sort the required data, one of these requests came in the form of sort a field based on the fields values in a given order, eg:

Status field can be 'New','Old','Replaced','Discarded'', by default an order by in MySQL on the field should return them in a alphabetically sorted way, to change this one can use the "order by field()" syntax:

order by field(Status, 'Old','Replaced','Discarded','New')

Very cool feature but unfortunately not always common knowledge, hopefully it saves someone else a bit of time!

No comments:

Post a Comment