I have a table in MYSQL on which I need to perform a select and then order it by a fairly complex logic. Here is an example of my logic:
if(column_a is not null && column_a > 0){
var_a = column_a
else
var_a = 1;
if(column_a is not null){
var_b = column_b
else
var_b = 0
if(column_c is not null){
var_c = column_c
else
var_c = column_d
ORDER BY (var_b*var_c)/var_a
So basically, I want to do something along the lines of SELECT * from mytable WHERE parameter = value ORDER BY variable_to_order_by . I would have to use some formula for variable_to_order_by, but I can’t find any way of doing it which would not involve eight separate CASE WHERE statements, and that seems extremely messy. Any other suggestions?
Thanks!
For a down and dirty way, you can use functions in an
order byclause. In specific, you can use theifandifnullfunctions. Therefore, you should be able to do something like this:(where the regular expression may have to be more complex if you want to include all reals instead of just integers, if you don’t want to include a string starting with 0, etc.)