zoukankan      html  css  js  c++  java
  • oracle order by 自定义

    我们通常需要根据客户需求对于查询出来的结果给客户提供自定义的排序方式,那么我们通常sql需要实现方式都有哪些,参考更多资料总结如下(不完善的和错误望大家指出):

    一、如果我们只是对于在某个程序中的应用是需要按照如下的方式排序,我们只需在SQL语句级别设置排序方式:  

    1、按照oracled的默认方式排序:select * from table_name order by  col_name  (desc|asc);(默认为升序或无序对于升降只有在数字字段);

    2、按照自定义的顺序排序:   select * from table_name order by  decode(col_name,'value1',1,value2',2,value3',3,value4',4,...valueN',N);

    二、如果我们只是对于在某个程序中的应用是需要按照如下设置的方式排序,我们只需在SQL语句级别设置排序方式(一般没有设置之前我们系统的中文默认排序方式是按照拼音排序   ):  

    1、按照拼音排序:   select * fromtable_name order by  nlssort(col_name,'NLS_SORT=SCHINESE_PINYIN_M');
    2、按照笔划排序:   select * from table_name order by nlssort(col_name,'NLS_SORT=SCHINESE_STROKE_M');  
    3、按照部首排序:   select * from table_name order by nlssort(col_name,'NLS_SORT=SCHINESE_RADICAL_M');  
    注意:但是在数据量比较大情况下查询速度会很慢,需要进行进一步优化,按Oracle官方文档的解释,oracle在对中文列建立索引时,是按照2进制编码进行排序的,所以如果NLS_SORT被设置为BINARY时,排序则可以利用索引.如果不是2进制排序,而是使用上面介绍的3种针对中文的特殊排序,则oracle无法使用索引,会进行全表扫描.解决方法是,在此列上建立linguistic index.例如:CREATE INDEX nls_index ON my_table (NLSSORT(name, 'NLS_SORT = SCHINESE_PINYIN_M'));

    引用原文:Note:

    Setting NLS_SORT to anything other than BINARY causes a sort to use a full table scan, regardless of the path chosen by the optimizer. BINARY is the exception because indexes are built according to a binary order of keys. Thus the optimizer can use an index to satisfy the ORDER BY clause when NLS_SORT is set to BINARY. If NLS_SORT is set to any linguistic sort, the optimizer must include a full table scan and a full sort in the execution plan.

    三、如果我们在整个会话中都要使用特定的方式排序的话,我们需要在Session级别的设置字段的默认排序方式:   
    1.按拼音:alter session set nls_sort = SCHINESE_PINYIN_M;   
    2.按笔画:alter session set nls_sort = SCHINESE_STROKE_M;    
    3.按偏旁:alter session set nls_sort = NLS_SORT=SCHINESE_RADICAL_M; 

    四、如果我们需要对整个数据做指定的排序方式,就需要修改oracle 服务器系统参数(一般这种方式我们使用的比较少):    
     1. win系统修改注册表 HKLCSOFTWAREORACLEhome0NLS_SORT 

    2.其他修改配置set NLS_SORT=SCHINESE_RADICAL_M export NLS_SORT

  • 相关阅读:
    let 和 const 命令
    字符串和数组之间相互转换
    angularjs 添加拦截器
    js判断数组是否包含指定元素的方法
    angularjs ng-include
    requirejs学习 关于requirejs的一些参数问题
    ngDialog 点击窗口以外不允许关闭弹窗
    jQuery Jcrop 图像裁剪
    Javascript模块化编程(三):require.js的用法
    angular.bootstrap手动加载angularjs模块
  • 原文地址:https://www.cnblogs.com/liangbo-/p/6529326.html
Copyright © 2011-2022 走看看