如何使用Oracle SQL查询首先按数字排序?

时间:2022-07-19 22:48:08

I have this table with a 'title' field which is varchar2 and I want to select all rows and sort them first by number and then by the alphabet as it normally happens.

我有一个带有'title'字段的表,它是varchar2,我想选择所有行,然后按编号排序,然后按字母顺序排序。

For instance, I currently get this using a simple ORDER BY title in the end:

例如,我目前最终使用一个简单的ORDER BY标题得到这个:

  • Abc
  • Def
  • 321

But I want this:

但我想要这个:

  • 321
  • Abc
  • Def

The weird thing is that SQL Developer shows the "right" order, with numbers first. But on my app (PHP using OCI8) it shows numbers last.

奇怪的是,SQL Developer显示“正确”的顺序,首先是数字。但是在我的应用程序(使用OCI8的PHP)上,它显示了最后的数字。

2 个解决方案

#1


12  

Not an Oracle expert, but you are supposed to be able to do it without altering the session with

不是Oracle专家,但您应该能够在不改变会话的情况下执行此操作

SELECT * FROM my_data SORT by NLSSORT(title,’NLS_SORT=BINARY_AI’)

where you can change the NLS_SORT= to fit your needs (here are the list of values)

您可以在哪里更改NLS_SORT =以满足您的需求(以下是值列表)

Keep in mind that docs says that this will force table scan, so it might be beneficial to filter them first (but if you are selecting all the table scan is what you are going to use anyway).

请记住,文档说这将强制进行表扫描,因此首先过滤它们可能是有益的(但如果选择所有表扫描,则无论如何都要使用它)。

The reason why SQL Developer exhibits different behaviour is probably because it changes the session.

SQL Developer表现出不同行为的原因可能是因为它改变了会话。

#2


5  

the difference in behaviour that you're seeing is probably because of different NLS_SORT parameter setting. Consider:

您所看到的行为差异可能是因为NLS_SORT参数设置不同。考虑:

SQL> select * from nls_session_parameters where parameter='NLS_SORT';

PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_SORT                       BINARY

SQL> SELECT * FROM my_data order by title;

TITLE
-----
321
Abc
Def

SQL> alter session set nls_sort=french;

Session altered

SQL> SELECT * FROM my_data order by title;

TITLE
-----
Abc
Def
321

You can build a query that should give you the expected result regardless of your NLS_SORT session parameter setting, for example:

无论您的NLS_SORT会话参数设置如何,您都可以构建一个可以提供预期结果的查询,例如:

SQL> SELECT *
  2    FROM my_data
  3   ORDER BY CASE
  4               WHEN regexp_like(title, '[0-9]+\.?[0-9]*') THEN
  5                1
  6               ELSE
  7                2
  8            END, title;

TITLE
-----
321
Abc
Def

#1


12  

Not an Oracle expert, but you are supposed to be able to do it without altering the session with

不是Oracle专家,但您应该能够在不改变会话的情况下执行此操作

SELECT * FROM my_data SORT by NLSSORT(title,’NLS_SORT=BINARY_AI’)

where you can change the NLS_SORT= to fit your needs (here are the list of values)

您可以在哪里更改NLS_SORT =以满足您的需求(以下是值列表)

Keep in mind that docs says that this will force table scan, so it might be beneficial to filter them first (but if you are selecting all the table scan is what you are going to use anyway).

请记住,文档说这将强制进行表扫描,因此首先过滤它们可能是有益的(但如果选择所有表扫描,则无论如何都要使用它)。

The reason why SQL Developer exhibits different behaviour is probably because it changes the session.

SQL Developer表现出不同行为的原因可能是因为它改变了会话。

#2


5  

the difference in behaviour that you're seeing is probably because of different NLS_SORT parameter setting. Consider:

您所看到的行为差异可能是因为NLS_SORT参数设置不同。考虑:

SQL> select * from nls_session_parameters where parameter='NLS_SORT';

PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_SORT                       BINARY

SQL> SELECT * FROM my_data order by title;

TITLE
-----
321
Abc
Def

SQL> alter session set nls_sort=french;

Session altered

SQL> SELECT * FROM my_data order by title;

TITLE
-----
Abc
Def
321

You can build a query that should give you the expected result regardless of your NLS_SORT session parameter setting, for example:

无论您的NLS_SORT会话参数设置如何,您都可以构建一个可以提供预期结果的查询,例如:

SQL> SELECT *
  2    FROM my_data
  3   ORDER BY CASE
  4               WHEN regexp_like(title, '[0-9]+\.?[0-9]*') THEN
  5                1
  6               ELSE
  7                2
  8            END, title;

TITLE
-----
321
Abc
Def