1. 程式人生 > >關於java中實現在oracle數據庫中實現對中文首字母進行排序的解決方案

關於java中實現在oracle數據庫中實現對中文首字母進行排序的解決方案

capital obj create team capi substr order ring 一個

首先介紹Oracle 9i新增加的一個系統自帶的排序函數
1、按首字母排序
在oracle9i中新增了按照拼音、部首、筆畫排序功能。設置NLS_SORT值
SCHINESE_RADICAL_M 按照部首(第一順序)、筆劃(第二順序)排序
SCHINESE_STROKE_M 按照筆劃(第一順序)、部首(第二順序)排序
SCHINESE_PINYIN_M 按照拼音排序
oracle9i中新增了按照拼音、部首、筆畫排序功能
用法示例:

Java代碼 技術分享
  1. 拼音
  2. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段,‘NLS_SORT = SCHINESE_PINYIN_M‘)
  3. 筆劃
  4. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段,‘NLS_SORT = SCHINESE_STROKE_M‘)
  5. 部首
  6. SELECT * FROM TEAM ORDER BY NLSSORT(排序字段,‘NLS_SORT = SCHINESE_RADICAL_M‘)


2、按首字母拼音查詢
首先我們要創建一個可以查詢漢字首字母的函數,函數代碼如下:

Java代碼 技術分享
  1. CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS
  2. V_COMPARE VARCHAR2(100);
  3. V_RETURN VARCHAR2(4000);
  4. FUNCTION F_NLSSORT(P_WORD IN VARCHAR2) RETURN VARCHAR2 AS
  5. BEGIN
  6. RETURN NLSSORT(P_WORD, ‘NLS_SORT=SCHINESE_PINYIN_M‘);
  7. END;
  8. BEGIN
  9. FOR I IN 1..LENGTH(P_NAME) LOOP
  10. V_COMPARE := F_NLSSORT(SUBSTR(P_NAME, I, 1));
  11. IF V_COMPARE >= F_NLSSORT(‘ 吖 ‘) AND V_COMPARE <= F_NLSSORT(‘驁 ‘) THEN
  12. V_RETURN := V_RETURN || ‘a‘;
  13. ELSIF V_COMPARE >= F_NLSSORT(‘八 ‘) AND V_COMPARE <= F_NLSSORT(‘簿 ‘) THEN
  14. V_RETURN := V_RETURN || ‘b‘;
  15. ELSIF V_COMPARE >= F_NLSSORT(‘嚓 ‘) AND V_COMPARE <= F_NLSSORT(‘錯 ‘) THEN
  16. V_RETURN := V_RETURN || ‘c‘;
  17. ELSIF V_COMPARE >= F_NLSSORT(‘咑 ‘) AND V_COMPARE <= F_NLSSORT(‘鵽 ‘) THEN
  18. V_RETURN := V_RETURN || ‘d‘;
  19. ELSIF V_COMPARE >= F_NLSSORT(‘妸 ‘) AND V_COMPARE <= F_NLSSORT(‘樲 ‘) THEN
  20. V_RETURN := V_RETURN || ‘e‘;
  21. ELSIF V_COMPARE >= F_NLSSORT(‘發 ‘) AND V_COMPARE <= F_NLSSORT(‘猤 ‘) THEN
  22. V_RETURN := V_RETURN || ‘f‘;
  23. ELSIF V_COMPARE >= F_NLSSORT(‘旮 ‘) AND V_COMPARE <= F_NLSSORT(‘腂 ‘) THEN
  24. V_RETURN := V_RETURN || ‘g‘;
  25. ELSIF V_COMPARE >= F_NLSSORT(‘妎 ‘) AND V_COMPARE <= F_NLSSORT(‘夻 ‘) THEN
  26. V_RETURN := V_RETURN || ‘h‘;
  27. ELSIF V_COMPARE >= F_NLSSORT(‘丌 ‘) AND V_COMPARE <= F_NLSSORT(‘攈 ‘) THEN
  28. V_RETURN := V_RETURN || ‘j‘;
  29. ELSIF V_COMPARE >= F_NLSSORT(‘哢 ‘) AND V_COMPARE <= F_NLSSORT(‘穒 ‘) THEN
  30. V_RETURN := V_RETURN || ‘k‘;
  31. ELSIF V_COMPARE >= F_NLSSORT(‘垃 ‘) AND V_COMPARE <= F_NLSSORT(‘擽 ‘) THEN
  32. V_RETURN := V_RETURN || ‘l‘;
  33. ELSIF V_COMPARE >= F_NLSSORT(‘嘸 ‘) AND V_COMPARE <= F_NLSSORT(‘椧 ‘) THEN
  34. V_RETURN := V_RETURN || ‘m‘;
  35. ELSIF V_COMPARE >= F_NLSSORT(‘拏 ‘) AND V_COMPARE <= F_NLSSORT(‘瘧 ‘) THEN
  36. V_RETURN := V_RETURN || ‘n‘;
  37. ELSIF V_COMPARE >= F_NLSSORT(‘筽 ‘) AND V_COMPARE <= F_NLSSORT(‘漚 ‘) THEN
  38. V_RETURN := V_RETURN || ‘o‘;
  39. ELSIF V_COMPARE >= F_NLSSORT(‘妑 ‘) AND V_COMPARE <= F_NLSSORT(‘曝 ‘) THEN
  40. V_RETURN := V_RETURN || ‘p‘;
  41. ELSIF V_COMPARE >= F_NLSSORT(‘七 ‘) AND V_COMPARE <= F_NLSSORT(‘裠 ‘) THEN
  42. V_RETURN := V_RETURN || ‘q‘;
  43. ELSIF V_COMPARE >= F_NLSSORT(‘亽 ‘) AND V_COMPARE <= F_NLSSORT(‘鶸 ‘) THEN
  44. V_RETURN := V_RETURN || ‘r‘;
  45. ELSIF V_COMPARE >= F_NLSSORT(‘仨 ‘) AND V_COMPARE <= F_NLSSORT(‘蜶 ‘) THEN
  46. V_RETURN := V_RETURN || ‘s‘;
  47. ELSIF V_COMPARE >= F_NLSSORT(‘侤 ‘) AND V_COMPARE <= F_NLSSORT(‘籜 ‘) THEN
  48. V_RETURN := V_RETURN || ‘t‘;
  49. ELSIF V_COMPARE >= F_NLSSORT(‘屲 ‘) AND V_COMPARE <= F_NLSSORT(‘鶩 ‘) THEN
  50. V_RETURN := V_RETURN || ‘w‘;
  51. ELSIF V_COMPARE >= F_NLSSORT(‘夕 ‘) AND V_COMPARE <= F_NLSSORT(‘鑂 ‘) THEN
  52. V_RETURN := V_RETURN || ‘x‘;
  53. ELSIF V_COMPARE >= F_NLSSORT(‘丫 ‘) AND V_COMPARE <= F_NLSSORT(‘韻 ‘) THEN
  54. V_RETURN := V_RETURN || ‘y‘;
  55. ELSIF V_COMPARE >= F_NLSSORT(‘帀 ‘) AND V_COMPARE <= F_NLSSORT(‘咗 ‘) THEN
  56. V_RETURN := V_RETURN || ‘z‘;
  57. END IF;
  58. END LOOP;
  59. RETURN V_RETURN;
  60. END;



隨後,我們可以先取出姓名,然後截取第一個漢字,最後取漢字的首字母,即可用來作為查詢條件

Java代碼 技術分享
  1. select e.fullname,e.expert_id from expert e where f_trans_pinyin_capital(substr(e.fullname,1,1)) = ‘z‘




最後展示一個將兩個結合起來運用的例子:

Java代碼 技術分享
    1. select e.expert_id,e.eperson_id,e.fullname,
    2. sum(b2i.browse_num) browsecount,
    3. count(o.object_id),wmsys.wm_concat(distinct d.name)
    4. from expert e
    5. left join expert2disciplinetype e2d on e2d.expert_id = e.expert_id
    6. left join disciplinetype d on d.discipline_type_id = e2d.discipline_type_id
    7. and d.upid = ‘0‘
    8. left join community2expert c2e on c2e.expert_id = e.expert_id
    9. left join item i on i.item_type_id = ‘AcademicRes‘ and i.withdrawn = ‘N‘
    10. join metadatavalue m on m.item_id = i.item_id and m.metadata_field_id = ‘64‘
    11. join objectpublishinfo o on o.object_id = i.item_id
    12. and o.object_type = ‘item‘ and o.viewobjecttype = ‘eperson‘
    13. and o.viewobjectid = e.eperson_id and o.state = ‘1‘
    14. and o.publishstate_id = ‘3‘
    15. left join browse2item b2i on b2i.item_id = o.object_id
    16. where e.state = ‘1‘ and f_trans_pinyin_capital(substr(e.fullname,1,1)) = ‘z‘
    17. group by e.expert_id,e.eperson_id,e.fullname
    18. order by nlssort(e.fullname,‘NLS_SORT=SCHINESE_PINYIN_M‘)

關於java中實現在oracle數據庫中實現對中文首字母進行排序的解決方案