B
B
bO_oblik2011-11-16 12:17:22
MySQL
bO_oblik, 2011-11-16 12:17:22

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 



Why might sorting by colors not work?

Clarification: the result without ORDER BY FIELD is the same, that is, this line is not taken into account at all.

Also, at the initial formation of the request, sorting worked.

Another clarification:

@color := IF( @price <>0, IF( @price < price_d,  "red", IF( @price <= price,  "yellow",  "green" ) ) ,  "white" ) AS color


If you do not use @price here, then it sorts normally :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
korvindest, 2011-11-16
@korvindest

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

V
Vitali Borovik, 2011-11-16
@WAYS

Doesn't work because you don't specify which table these fields are in.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question