MySQL:按用户定义的方式查询。

时间:2022-06-08 16:41:37

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:

fld_type_id的顺序是1 2 3 4,但我想要的是在3 4 2 1中安排这个顺序,我试着这样做

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?

但它没有帮助我……我该怎么做呢?

2 个解决方案

#1


1  

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.

当fld_type_id不在列表中时,它会出现在下面。

#2


1  

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

#1


1  

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.

当fld_type_id不在列表中时,它会出现在下面。

#2


1  

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