Monday, 16 July 2018

Order MySQL Query by User Defined Order Using the Field() Function

  
The way to order this using the order ‘one’, ‘two’, ‘three’, ‘four’ is to use the MySQL Field() function. Using Field() it is possible to define the order which will return the index of the string position that matches string. The MySQL documentation is incomplete with using Field() in conjunction with ORDER BY, but this example illustrates the syntax.
  
The strings used to ORDER BY do not need to represent numbers. In my challenge I needed to order the results by a status column that contained a number of different strings and business rules dictated the order of importance for the MySQL query in question.

0 comments:

Post a Comment