如何在Oracle中通过越南字母排序

时间:2018-04-06 01:55:53

标签: sql oracle

我有如下数据库:

    WITH TB AS(
     SELECT 1 ID, N'Bấm kim' NAMES FROM DUAL UNION ALL 
     SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES  FROM DUAL 
     UNION ALL
     SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp' NAMES  FROM DUAL 
     UNION ALL
     SELECT 4 ID, N'Ăn kem' NAMES  FROM DUAL 
)
SELECT * FROM TB ORDER BY NAMES ASC

如何通过带有VietNamese字母的NAMES列进行订购。像

AĂ,B,C,D,......我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

一种方法是设置会话参数NLS_SORT。另一种方法是通过NLSSORT(......,...)订购(见下文)。

第一种方法,改变会话的NLS_SORT

比较

alter session set nls_sort = German;

Session altered.

WITH TB AS(
  SELECT 1 ID, N'Bấm kim'                            NAMES FROM DUAL UNION ALL 
  SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES FROM DUAL UNION ALL
  SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp'  NAMES FROM DUAL UNION ALL
  SELECT 4 ID, N'Ăn kem'                             NAMES FROM DUAL 
)
SELECT * FROM TB ORDER BY NAMES ASC;

   ID NAMES                             
----- ----------------------------------
    1 Bấm kim                           
    3 Đục lỗ, gắn, đóng nút da trên nắp 
    4 Ăn kem                            
    2 Quét keo, dán mylar da đệm mắt cáo

VS

alter session set nls_sort = Vietnamese;

Session altered.

WITH TB AS(
  SELECT 1 ID, N'Bấm kim'                            NAMES FROM DUAL UNION ALL 
  SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES FROM DUAL UNION ALL
  SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp'  NAMES FROM DUAL UNION ALL
  SELECT 4 ID, N'Ăn kem'                             NAMES FROM DUAL 
)
SELECT * FROM TB ORDER BY NAMES ASC;

   ID NAMES                             
----- ----------------------------------
    4 Ăn kem                            
    1 Bấm kim                           
    3 Đục lỗ, gắn, đóng nút da trên nắp 
    2 Quét keo, dán mylar da đệm mắt cáo

第二种方法,仅使用越南语进行此查询(请参阅ORDER BY子句!)

WITH TB AS(
  SELECT 1 ID, N'Bấm kim'                            NAMES FROM DUAL UNION ALL 
  SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES FROM DUAL UNION ALL
  SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp'  NAMES FROM DUAL UNION ALL
  SELECT 4 ID, N'Ăn kem'                             NAMES FROM DUAL 
)
SELECT * FROM TB ORDER BY NLSSORT(NAMES, 'nls_sort = Vietnamese') ASC;

   ID NAMES                             
----- ----------------------------------
    4 Ăn kem                            
    1 Bấm kim                           
    3 Đục lỗ, gắn, đóng nút da trên nắp 
    2 Quét keo, dán mylar da đệm mắt cáo

答案 1 :(得分:1)

使用NLSSORT功能 https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions104.htm#SQLRF00678

   WITH TB AS(
     SELECT 1 ID, N'Bấm kim' NAMES FROM DUAL UNION ALL 
     SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES  FROM DUAL 
     UNION ALL
     SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp' NAMES  FROM DUAL 
     UNION ALL
     SELECT 4 ID, N'Ăn kem' NAMES  FROM DUAL 
)
SELECT * FROM TB ORDER BY nlssort(NAMES,'NLS_SORT = VIETNAMESE') ASC;

答案 2 :(得分:0)

您可以尝试ORDER BY这样的CASE表达式使用WITH TB AS( SELECT 1 ID, N'Bấm kim' NAMES FROM DUAL UNION ALL SELECT 2 ID, N'Quét keo, dán mylar da đệm mắt cáo' NAMES FROM DUAL UNION ALL SELECT 3 ID, N'Đục lỗ, gắn, đóng nút da trên nắp' NAMES FROM DUAL UNION ALL SELECT 4 ID, N'Ăn kem' NAMES FROM DUAL ) SELECT * FROM TB ORDER BY CASE WHEN NAMES LIKE 'Â%' OR NAMES LIKE 'Ă%' OR NAMES LIKE 'A%' THEN 1 WHEN NAMES LIKE 'B%' THEN 2 WHEN NAMES LIKE 'C%' THEN 3 WHEN NAMES LIKE 'D%' THEN 4 WHEN NAMES LIKE 'Đ%' THEN 5 .....etc END ASC 越南字母。

if (isset($_GET['party'])) {
    echo $_GET['party'];
}
if (isset($_GET['partytwo'])) {
    echo $_GET['partytwo'];
}
......

SQLFiddle