I’ve got a result set I have to sort in quite a complex way. There are two fields (A and B) which are employed in the sorting, and the algorithm is like this:
- Sort by field A
- Then, if the field A contains a substring K, sort by field B using a custom function F (see below). If the field A doesn’t contain K, the built-in sorting for B is just fine.
More information:
- The field A is a VARCHAR.
- The field B is an INTEGER that can have the following values: {1, 2, 3, 4, 5}.
- Function F is simple as “sort values in this way: {4, 5, 1, 2, 3}”.
How can I do this in MySQL through a SQL query? Am I forced to do it cient-side?
1 Answer