数据库为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)

 

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-12-21
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-12
  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案