'ordinals' 在带有 CASE 条件的 ORDER BY 子句中的意义是什么
What is the significance of 'ordinals' in an ORDER BY clause with CASE conditions
我有一个包含两列的 'names' 表:last_name、first_name
last_name first_name
--------- ----------
Basu Rohini
Khan Amartya
Nandy Upanita
Ghosh Shankha
NULL Claire
NULL Amelie
当我做的时候(第一个SQL):
SELECT last_name, first_name FROM names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 3
ELSE 100
END,
last_name DESC
我得到一个输出:
last_name first_name
--------- ----------
Nandy Upanita
Khan Amartya
Ghosh Shankha
Basu Rohini
NULL Amelie
NULL Claire
但是当我这样做时(第 2 个 SQL):
SELECT last_name, first_name from names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 3
END,
last_name DESC
或者这个(第 3 SQL):
SELECT last_name, first_name from names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 100
ELSE 3
END,
last_name DESC
我得到的输出以 last_name 作为 NULL 开头,first_name 以升序排列,然后是非空 last_name 以降序排列:
last_name first_name
--------- ----------
NULL Amelie
NULL Claire
Nandy Upanita
Khan Amartya
Ghosh Shankha
Basu Rohini
我的问题:
为什么第一个 SQL 给出的输出 last_name 以降序方式排序,对于非空姓氏?
为什么我在第一个 SQL 中没有收到错误 'CASE WHEN last_name IS NOT NULL THEN 3' 说 'Expressions 3 and last_name in ORDER BY clause are same'?
为什么我能够在 CASE 语句下给出任何随机数,例如 3 和 100?根据 SQL 标准,此数字只能是不超过列数的任何非负整数值。 CASE 语句中的整数如何工作?
这些不是序数 - 它们只是数字。您的每个 CASE
表达式本质上都归结为 "assign all of the rows with a NULL
last name <some fixed value>
and all of the non-NULL
last name rows <some other fixed value>
",然后对这些固定值执行排序。
所以这些 CASE
表达式所做的就是确保所有 NULL
行都将出现 before/after 非 NULL 行。具体采用哪种方法取决于您在每个查询中使用的特定固定值。
Why is the 1st SQL giving an output with last_name sorted in a descending manner, for non-null last names?
因为那是 ORDER BY
子句中的第二个表达式所要求的?
Why I'm not getting an error in 1st SQL for 'CASE WHEN last_name IS NOT NULL THEN 3' saying 'Expressions 3 and last_name in ORDER BY clause are same'?
因为表达式不相同,即使它们相同,SQL 也不会阻止您要求 redundant/pointless 排序(即您可以在 ORDER BY
子句比唯一确定每一行最终输出位置所需的子句更不会产生错误)
Why I was able to give any random numbers like 3 and 100, under the CASE statement? As per the SQL standard, this number can only be any non-negative integer value upto the degree of the number of columns. How is the integer within CASE statement working?
因为,同样,这些不是序数。为了被视为序数,您必须提供 constant 文字整数作为整个 order by expression
- 而不是更大的表达式,例如这些 CASE
表达式。
我有一个包含两列的 'names' 表:last_name、first_name
last_name first_name
--------- ----------
Basu Rohini
Khan Amartya
Nandy Upanita
Ghosh Shankha
NULL Claire
NULL Amelie
当我做的时候(第一个SQL):
SELECT last_name, first_name FROM names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 3
ELSE 100
END,
last_name DESC
我得到一个输出:
last_name first_name
--------- ----------
Nandy Upanita
Khan Amartya
Ghosh Shankha
Basu Rohini
NULL Amelie
NULL Claire
但是当我这样做时(第 2 个 SQL):
SELECT last_name, first_name from names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 3
END,
last_name DESC
或者这个(第 3 SQL):
SELECT last_name, first_name from names
ORDER BY
CASE
WHEN last_name IS NOT NULL THEN 100
ELSE 3
END,
last_name DESC
我得到的输出以 last_name 作为 NULL 开头,first_name 以升序排列,然后是非空 last_name 以降序排列:
last_name first_name
--------- ----------
NULL Amelie
NULL Claire
Nandy Upanita
Khan Amartya
Ghosh Shankha
Basu Rohini
我的问题:
为什么第一个 SQL 给出的输出 last_name 以降序方式排序,对于非空姓氏?
为什么我在第一个 SQL 中没有收到错误 'CASE WHEN last_name IS NOT NULL THEN 3' 说 'Expressions 3 and last_name in ORDER BY clause are same'?
为什么我能够在 CASE 语句下给出任何随机数,例如 3 和 100?根据 SQL 标准,此数字只能是不超过列数的任何非负整数值。 CASE 语句中的整数如何工作?
这些不是序数 - 它们只是数字。您的每个 CASE
表达式本质上都归结为 "assign all of the rows with a NULL
last name <some fixed value>
and all of the non-NULL
last name rows <some other fixed value>
",然后对这些固定值执行排序。
所以这些 CASE
表达式所做的就是确保所有 NULL
行都将出现 before/after 非 NULL 行。具体采用哪种方法取决于您在每个查询中使用的特定固定值。
Why is the 1st SQL giving an output with last_name sorted in a descending manner, for non-null last names?
因为那是 ORDER BY
子句中的第二个表达式所要求的?
Why I'm not getting an error in 1st SQL for 'CASE WHEN last_name IS NOT NULL THEN 3' saying 'Expressions 3 and last_name in ORDER BY clause are same'?
因为表达式不相同,即使它们相同,SQL 也不会阻止您要求 redundant/pointless 排序(即您可以在 ORDER BY
子句比唯一确定每一行最终输出位置所需的子句更不会产生错误)
Why I was able to give any random numbers like 3 and 100, under the CASE statement? As per the SQL standard, this number can only be any non-negative integer value upto the degree of the number of columns. How is the integer within CASE statement working?
因为,同样,这些不是序数。为了被视为序数,您必须提供 constant 文字整数作为整个 order by expression
- 而不是更大的表达式,例如这些 CASE
表达式。