Answer the question
In order to leave comments, you need to log in
Sorting a complex query
For example, there is a mega-request:
SELECT `good` . * , recovery_min, recovery_max,
@price := IF( price_c <>0, price_c, IF( mid_price <>0, mid_price, 0 ) ) AS con_price,
@color := IF( @price <>0, IF( @price < price_d, "red", IF( @price <= price, "yellow", "green" ) ) , "white" ) AS color,
TRUNCATE( IF( @color = "red", recovery_min, IF( @color = "yellow", IF( (
@price - @price * 0.002
) / price_d < recovery_min, recovery_min, (
@price - @price * 0.002
) / price_d ) , IF( @color = "green", (
@price - @price * 0.002
) / price_d, recovery_max ) ) ) , 4 ) AS result,
FROM `goods` AS `good`
JOIN `categories` ON ( `categories`.`cat_id` = `good`.`cat_id` )
WHERE `good`.`cat_id` = '11'
ORDER BY FIELD( `color` , "red", "yellow", "green", "white" ) ASC
@color := IF( @price <>0, IF( @price < price_d, "red", IF( @price <= price, "yellow", "green" ) ) , "white" ) AS color
Answer the question
In order to leave comments, you need to log in
You haven't written a DBMS, so it's a bit more difficult to advise you, but you can try this:
ORDER BY CASE
WHEN color = "red" THEN 1
WHEN color = "yellow" THEN 2
ELSE 3 END
Doesn't work because you don't specify which table these fields are in.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question