简体   繁体   English

为什么在Oracle和Mysql中不支持在同一选择中使用列别名?

[英]Why use of column alias in same select is not supported in Oracle and Mysql?

create table t1 (c1 integer);

select c1*3 temp, case when (temp <>3) then 1 else 0 end from t1;

Query fails in both Oracle and MySQL But why they doesn't support this type of queries? 在Oracle和MySQL中查询均失败,但是为什么它们不支持这种查询呢?

I already went through the answers related to column alias. 我已经经历了与列别名相关的答案。 Everywhere its explained what is supported and what is not and reasoning about why not in where clause. 它在任何地方都在where子句中说明了支持什么,不支持什么以及原因。 But question is about why its not supported in select clause. 但是问题是为什么select子句中不支持它。

An alias can be used in a query select list to give a column a different name. 可以在查询选择列表中使用别名来为列指定其他名称。 You can use the alias in GROUP BY , ORDER BY , or HAVING clauses to refer to the column: 您可以在GROUP BYORDER BYHAVING子句中使用别名来引用该列:

SELECT SQRT(a*b) AS root FROM tbl_name
  GROUP BY root HAVING root > 0;

SELECT id, COUNT(*) AS cnt FROM tbl_name
  GROUP BY id HAVING cnt > 0;

SELECT id AS 'Customer identity' FROM tbl_name;

Standard SQL disallows references to column aliases in a WHERE clause. 标准SQL不允许在WHERE子句中引用列别名。 This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. 之所以施加此限制,是因为在评估WHERE子句时,可能尚未确定列值。 For example, the following query is illegal: 例如,以下查询是非法的:

SELECT id, COUNT(*) AS cnt FROM tbl_name
  WHERE cnt > 0 GROUP BY id;

The WHERE clause determines which rows should be included in the GROUP BY clause, but it refers to the alias of a column value that is not known until after the rows have been selected, and grouped by the GROUP BY . WHERE子句确定应在GROUP BY子句中包括哪些行,但它引用的是列值的别名,该别名直到选择了行并由GROUP BY分组后才知道。

In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters: 在查询的选择列表中,可以使用标识符或字符串引号指定带引号的列别名:

SELECT 1 AS `one`, 2 AS 'two';

Elsewhere in the statement, quoted references to the alias must use identifier quoting or the reference is treated as a string literal. 在语句的其他地方,带引号的别名引用必须使用标识符引号,否则该引用将被视为字符串文字。 For example, this statement groups by the values in column id, referenced using the alias a : 例如,该语句将列ID中的值分组,并使用别名a引用:

SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
  GROUP BY `a`;

But this statement groups by the literal string 'a' and will not work as expected: 但是此语句按文字字符串“ a”分组,将无法按预期工作:

SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
  GROUP BY 'a';

Source: https://docs.oracle.com/cd/E17952_01/refman-5.0-en/problems-with-alias.html 资料来源: https : //docs.oracle.com/cd/E17952_01/refman-5.0-zh/problems-with-alias.html

You can't use aliases in any sections except ORDER BY (in Oracle) ORDER BY (在Oracle中)外,您不能在任何节中使用别名

So you can do either: 因此,您可以执行以下任一操作:

select c1*3 temp, case when (c1*3 <>3) then 1 else 0 end from t1;

Or: 要么:

select temp, case when (temp <>3) then 1 else 0 end 
from (
select c1*3 temp from t1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM