Question

I have a query where i get data like this:

fld_id  | fld_type_id | fld_product
-----------------------------------
    81  |   1         |    25
    82  |   2         |    25
    83  |   3         |    25
    84  |   4         |    25

The order of fld_type_id is 1,2,3,4 but what I want is to arrange this order in 3, 4, 2, 1 I tried to do this as:

Select * from tbl 
where fld_product = 25 
Order by Case fld_type_id 
when 3 then 1 
when 4 then 2 
when 2 then 3 
else 4

but it did'nt helped me... How can I do this?

Was it helpful?

Solution

use FIELD()

ORDER BY FIELD(fld_type_id, 1, 2, 4, 3) DESC

when fld_type_id is not in the list, it appears below.

OTHER TIPS

try

Select *, 
Case fld_type_id when 3 then 1 
when 4 then 2 
when 2 then 3 
else 4 END as sOrder 
from tbl where fld_product = 25 Order by sOrder
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top