gd1
gd1

Reputation: 11403

How do I conditionally sort on a second column using a custom MySQL function?

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:

  1. Sort by field A
  2. 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:

How can I do this in MySQL through a SQL query? Am I forced to do it cient-side?

Upvotes: 0

Views: 108

Answers (1)

Andriy M
Andriy M

Reputation: 77707

…
ORDER BY
  A,
  CASE WHEN A LIKE CONCAT('%', K, '%') THEN Func(B) ELSE B END

Upvotes: 3

Related Questions