使用order by 按照指定顺序排序或自定义顺序排序
2017-11-03 15:14
537 查看
我们通常需要根据客户需求对于查询出来的结果给客户提供自定义的排序方式,那么我们通常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系统修改注册表 HKLC\SOFTWARE\ORACLE\home0\NLS_SORT
2.其他修改配置set NLS_SORT=SCHINESE_RADICAL_M export NLS_SORT
一、如果我们只是对于在某个程序中的应用是需要按照如下的方式排序,我们只需在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系统修改注册表 HKLC\SOFTWARE\ORACLE\home0\NLS_SORT
2.其他修改配置set NLS_SORT=SCHINESE_RADICAL_M export NLS_SORT
相关文章推荐
- SQL学习之使用order by 按照指定顺序排序或自定义顺序排序
- SQL学习之使用order by 依照指定顺序排序或自己定义顺序排序
- SqlServer按照指定顺序对字段进行排序
- SQL按照指定顺序对字段进行排序
- MySql查询结果按照指定顺序排序
- MySql查询结果按照指定顺序排序
- SqlServer按照指定顺序对字段进行排序
- 通过Order by按指定顺序排序
- Oracle中使用Order By排序时结果顺序不稳定的解决办法
- SQL Server中使用order by charindex按指定顺序排序
- 在TreeSet中实现存储自定义对象,并且能够按照特定的顺序排序
- 按照指定顺序排序
- sql server 根据指定字段排序编号 update 与 order by 联合使用
- 对聚集表查询的时候,未显式指定排序列的时候,默认查询结果的顺序一定是按照聚集索引顺序排序的吗
- 使用Arrays.sort()对自定义的entity按指定属性进行排序。
- PHP 多维数组按照指定的顺序进行排序
- 按照指定的顺序对字段值进行连接--聚合函数的使用
- Order by 按指定顺序进行排序
- MySql查询结果按照指定顺序排序
- 使用Word对段落按照字母顺序进行排序