使用sql query oracle按字母顺序获取记录

ntjbwcob  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(121)

我想得到输出的y选择列表的字母顺序,下面是我的选择查询

select name, id from hes_std_name_id
where id = 121
order by name asc;

下面是我的输出:
| |
| --------------|
| AHAS|
| 贝塔|
| aa|
| 生物|
| 现金|
| 存款|
我想要的输出如下:
| |
| --------------|
| aa|
| AHAS|
| 贝塔|
| 生物|
| 现金|
| 存款|

8qgya5xd

8qgya5xd1#

看起来您只想执行不区分大小写的排序。最简单的选择是

order by upper(name) asc

您还可以考虑将会话的nls_compnls_sort设置为LINGUISTICBINARY_CI,以便所有查询都进行不区分大小写的搜索和排序。但这会产生更广泛的影响,包括优化器使用在具有不同nls_compnls_sort设置的会话中创建的索引的能力。

btqmn9zl

btqmn9zl2#

试着通过ASCII表来查看它。如果您的示例数据是:

WITH
    tbl (A_TEXT) AS
        (
            Select 'AHAS'    From Dual Union All
            Select 'BETA'    From Dual Union All
            Select 'aa'      From Dual Union All
            Select 'bios'    From Dual Union All
            Select 'cash'    From Dual Union All
            Select 'deposit' From Dual 
        )

然后(对于前两个字母)你的查询的ASCII表示,如问题中的Order By子句,将如下所示:

--  Your Order By clause
Select     A_TEXT,
          '--->' "AS_IS",
          SubStr(A_TEXT, 1, 1) || '_' || SubStr(A_TEXT, 2, 1) "TXT",
          ASCII(SubStr(A_TEXT, 1, 1))  || '_' || ASCII(SubStr(A_TEXT, 2, 1)) "AS_IS_ASCII"
From      tbl
Order By  A_TEXT

A_TEXT  AS_IS TXT AS_IS_ASCII 
------- ----- --- ------------
AHAS    --->  A_H 65_72       
BETA    --->  B_E 66_69       
aa      --->  a_a 97_97       
bios    --->  b_i 98_105      
cash    --->  c_a 99_97       
deposit --->  d_e 100_101

...但是如果你在Order By子句中使用Upper()或Lower()函数,它会像下面这样(又是第一个两个字母):

-- Using Upper() or Lower()
Select    ASCII(SubStr(Upper(A_TEXT), 1, 1)) || '_' || ASCII(SubStr(Upper(A_TEXT), 2, 1)) "UPPER_ASCII",
          SubStr(Upper(A_TEXT), 1, 1) || '_' || SubStr(Upper(A_TEXT), 2, 1) "UPP",
          '<---' "UPPER",
          A_TEXT,
          '--->' "LOWER",
          SubStr(Lower(A_TEXT), 1, 1) || '_' || SubStr(Lower(A_TEXT), 2, 1) "LOW",
          ASCII(SubStr(Lower(A_TEXT), 1, 1))  || '_' || ASCII(SubStr(Lower(A_TEXT), 2, 1)) "LOWER_ASCII"
From      tbl
Order By  Lower(A_TEXT)  -- or Upper(A_TEXT)

UPPER_ASCII     UPP UPPER  A_TEXT  LOWER  LOW    LOWER_ASCII 
--------------- --- ----  ------- ------- ------ ----------------
65_65           A_A <---    aa    --->    a_a    97_97           
65_72           A_H <---   AHAS   --->    a_h    97_104          
66_69           B_E <---   BETA   --->    b_e    98_101          
66_73           B_I <---   bios   --->    b_i    98_105          
67_65           C_A <---   cash   --->    c_a    99_97           
68_69           D_E <---  deposit --->    d_e    100_101

相关问题