SQL自定义排序

数据库为Oracle,针对表table_example的class字段排序,class字段值为:A、B、C、D。

用户要求table_example中的数据按照class字段值C、A、D、B的顺序排序。

方法一:

开始想了一个方法是select的时候增加一个自定义字段custom,当class的值为C、A、D、B时令custom的值为1、2、3、4.

利用case when时,发现无需增加自定义字段即可实现:

select * from teble_exaple order by
(
    case class 
        when 'C' then 1,
        when 'A' then 2,
        when 'D' then 3,
        when 'B' then 4
    else '' end
)

方法二:

利用decode函数:

select * from table_example order by decode(class,'C',1,'A',2,'D',3,'B',4)

 

posted on 2013-05-23 17:31  独臂刀客  阅读(719)  评论(0)    收藏  举报

导航