oracle 在选择查询中使用别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7994408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:25:11  来源:igfitidea点击:

Use Alias in Select Query

sqlsql-serveroracle

提问by user960567

I need to ask how can use Alias in Select Query,

我需要问如何在选择查询中使用别名,

I need this

我需要这个

SELECT (Complex SubQuery) AS A, (Another Sub Query WHERE ID = A) FROM TABLE

回答by Johan

You cannot do this:

你不可以做这个:

SELECT (Complex SubQuery) AS A, (Another Sub Query WHERE ID = A) FROM TABLE

You can however do this:

但是,您可以这样做:

SELECT (Another Sub Query WHERE ID = A.somecolumn)
FROM table
JOIN SELECT (Complex SubQuery) AS A on (A.X = TABLE.Y)

Or

或者

SELECT (Another Sub Query)
FROM table
WHERE table.afield IN (SELECT Complex SubQuery.otherfield)

The problem is that you cannot refer to aliases like this in the SELECT and WHERE clauses, because they will not have evaluated by the time the select or where part is executed.
You can also use a havingclause, but having clauses do not use indexes and should be avoided if possible.

问题是你不能在 SELECT 和 WHERE 子句中引用这样的别名,因为在执行 select 或 where 部分时它们不会被评估。
您也可以使用having子句,但具有子句不使用索引,应尽可能避免使用。

回答by wqw

You can rewrite your query like this

你可以像这样重写你的查询

SELECT Complex.A, (Another Sub Query WHERE ID = Complex.A)
FROM TABLE
CROSS JOIN ((Complex SubQuery) AS A) Complex

回答by Arbel Sella

Another solution which you can use:

您可以使用的另一种解决方案:

SELECT (Complex SubQuery) AS A, (Another Sub Query WHERE ID = A) 
FROM 
TABLE MAIN
OUTER APPLY (SELECT (Complex SubQuery) AS A)