一聚教程网:一个值得你收藏的教程网站

热门教程

Oracle数据库汉字中文字段order by排序的方法总结

时间:2022-06-29 09:36:26 编辑:袖梨 来源:一聚教程网

一,最常用的排序方法

1)按笔画排序

select * from Table order by nlssort(columnName,'NLS_SORT=SCHINESE_STROKE_M')

2)按部首排序

select * from Table order by nlssort(columnName,'NLS_SORT=SCHINESE_RADICAL_M')

3)按拼音排序

select * from Table order by nlssort(columnName,'NLS_SORT=SCHINESE_PINYIN_M');

补充:

1.在Oracle的官方文档中关于排序有如下描述


其中表“Table A-15 Multilingual LInguistic Sorts”中我们关注一下有关中文排序的内容(前三条与简体中文排序有关,后两条与繁体中文排序有关):

1)SCHINESE_RADICAL_M
Simplified Chinese sort based on radical as primary order and number of strokes order as secondary order
注释:简体中文按照第一顺序是“部首”第二顺序是“笔画数”进行排序;

2)SCHINESE_STROKE_M
Simplified Chinese sort uses number of strokes as primary order and radical as secondary order
注释:简体中文按照第一顺序“笔画数”是第二顺序是“部首”进行排序;

3)SCHINESE_PINYIN_M
Simplified Chinese PinYin sorting order
注释:简体中文按照“拼音”进行排序;

4)TCHINESE_RADICAL_M
Traditional Chinese sort based on radical as primary order and number of strokes order as secondary order
注释:繁体中文按照第一顺序是“部首”第二顺序是“笔画数”进行排序;

5)TCHINESE_STROKE_M
Traditional Chinese sort uses number of strokes as primary order and radical as secondary order. It supports supplementary characters.
注释:繁体中文按照第一顺序“笔画数”是第二顺序是“部首”进行排序;

2.创建实验表T,并初始化六条记录
sec@secooler> create table t (x varchar2(10));
sec@secooler> insert into t values ('侯');
sec@secooler> insert into t values ('你');
sec@secooler> insert into t values ('做');
sec@secooler> insert into t values ('拉');
sec@secooler> insert into t values ('推');
sec@secooler> insert into t values ('拆');
sec@secooler> commit;
sec@secooler> select * from t;

X
------------------------------





6 rows selected.

3.确认数据库版本和数据库字符集
sec@secooler> select * from v$version;

BANNER
----------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

sec@secooler> select userenv('language') from dual;

USERENV('LANGUAGE')
-----------------------------------------------------------
AMERICAN_AMERICA.ZHS16GBK

4.在此环境下,查看默认的汉字排序规则
sec@secooler> select * from t order by x;

X
------------------------------





6 rows selected.

可见,此时的默认汉字排序规则是“拼音”。

5.使用nlssort强制按照拼音排序的方法
sec@secooler> select * from t order by nlssort(x,'NLS_SORT=SCHINESE_PINYIN_M');

X
------------------------------





6 rows selected.

6.使用nlssort强制按照“部首”(第一顺序)和“笔画数”(第二顺序)排序的方法
sec@secooler> select * from t order by nlssort(x,'NLS_SORT=SCHINESE_RADICAL_M');

X
------------------------------





6 rows selected.

单人旁的汉字在前,提手旁汉字在后;单人旁的三个汉字进一步又是按照笔画数多少进行的排序。

7.使用nlssort强制按照“笔画数”(第一顺序)和“部首”(第二顺序)排序的方法
sec@secooler> select * from t order by nlssort(x,'NLS_SORT=SCHINESE_STROKE_M');

X
------------------------------





6 rows selected.

可见,越往后的汉字的笔画数越多。

热门栏目