oracle order by 排序

oracle order by 排序

ORDER BY { column-Name | ColumnPosition | Expression }
    [ ASC | DESC ]
    [ NULLS FIRST | NULLS LAST ]
    [ , column-Name | ColumnPosition | Expression 
    [ ASC | DESC ]
    [ NULLS FIRST | NULLS LAST ]
    ] * 

知识点

  • order by 后面可以接列号(数字)、列名、别名、表达式、函数、分组函数
  • order by 对空值的处理,DESC空值在前,ASC空值在后;
  • order by子句中可以不含select中的列;
  • 当使用select distinctgroup by时,order by 不能使用select之外的列;
  • order by 只能放最后,不能放集合操作的中间;
  • 集合操作后,不接order by时按第一列进行升序排序(union all除外);
  • 集合操作后的列名为第一个select的内容,order by 只能选第一个select中的内容进行操作

 

【Oracle 结论】 
order by colum asc 时,null默认被放在最后
order by colum desc 时,null默认被放在最前
nulls first 时,强制null放在最前,不为null的按声明顺序[asc|desc]进行排序
nulls last 时,强制null放在最后,不为null的按声明顺序[asc|desc]进行排序 

针对【oracle】我们就需要使用以下语法:order by order_col [asc|desc] nulls [first|last]

 

laravel中orderby时为空字段排在最后面

$query->orderBy(DB::Raw(‘case when distance is null then 99999999999 else distance end’),‘ASC’);

 

手动排序:

select * from table  order by decode(confirm_branchid,'外科',1,'内科',2,'手术室',3,'化疗室',4)

posted @ 2020-08-03 16:45  水兮云间  阅读(435)  评论(1编辑  收藏  举报