oracle 带有order by的SELECT语句返回错误的order [重复]

pxyaymoc  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(114)

此问题已在此处有答案

how to sort by case insensitive alphabetical order using COLLATE NOCASE(3个答案)
Order by case insensitive in oracle(2个答案)
上个月关门了。
我想按L5_ORDERBY表的名称列获得结果顺序,但它给了我错误的顺序

CREATE TABLE L5_ORDERBY 
(
     LVL NUMBER, 
     NAME VARCHAR2(10)
);

INSERT INTO L5_ORDERBY VALUES (-20, 'j');
INSERT INTO L5_ORDERBY VALUES (-30, 'H');
INSERT INTO L5_ORDERBY VALUES (12, 'G');
INSERT INTO L5_ORDERBY VALUES (30, 'f');
INSERT INTO L5_ORDERBY VALUES (1, 'e');
INSERT INTO L5_ORDERBY VALUES (1, 'e');
INSERT INTO L5_ORDERBY VALUES (6, 'E');
INSERT INTO L5_ORDERBY VALUES (1, 'E');
INSERT INTO L5_ORDERBY VALUES (4, 'c');
INSERT INTO L5_ORDERBY VALUES (3, 'c');
INSERT INTO L5_ORDERBY VALUES (5, 'b');

SELECT * 
FROM L5_ORDERBY 
ORDER BY NAME DESC;

这是我得到的结果:
| LVL|名称|
| --|--|
| -20| J|
| 30 |H|
| 12 |G|
| 30 |F|
| 1 |e|
| 1 |e|
| 6 |E|
| 1 |E|
| 4 |C|
| 3 |C|
| 5 |B|
Oracle返回的结果按名称排序错误

t5fffqht

t5fffqht1#

是的;只要你具体说明“错误顺序”是什么意思。我假设你想做区分大小写排序。如果是这样,那么:
您当前的输出:

SQL> select * from l5_orderby order by name desc;

       LVL NAME
---------- ----------
       -20 j
       -30 H
        12 G
        30 f
         1 e
         1 e
         6 E
         1 E
         4 c
         3 c
         5 b

11 rows selected.

更改nls_sort

SQL> alter session set nls_sort = binary;

Session altered.

新结果:

SQL> select * from l5_orderby order by name desc;

       LVL NAME
---------- ----------
       -20 j
        30 f
         1 e
         1 e
         3 c
         4 c
         5 b
       -30 H
        12 G
         6 E
         1 E

11 rows selected.

SQL>

如果要恢复更改,则

alter session set nls_sort = binary_ci;

更多关于它的文档。
如果你在12 c及以上,使用collate

SQL> select * from l5_orderby order by name collate binary desc;

       LVL NAME
---------- ----------
       -20 j
        30 f
         1 e
         1 e
         3 c
         4 c
         5 b
       -30 H
        12 G
         6 E
         1 E

11 rows selected.

SQL>

相关问题