为什么 Oracle SQL 不允许我们在条件中使用别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2235883/
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
Why won't Oracle SQL let us use aliases in conditions?
提问by Mehper C. Palavuzlar
Oracle PL/SQL won't let users to use aliases in conditions. In most cases it's very practical for me to use aliases instead of long statements. What's the reason for that? What bad would happen if we could use aliases in conditions?
Oracle PL/SQL 不允许用户在条件中使用别名。在大多数情况下,使用别名而不是长语句对我来说非常实用。这是什么原因?如果我们可以在条件中使用别名,会发生什么坏事?
Example case: What's wrong with this SQL query?
示例案例:这个 SQL 查询有什么问题?
回答by Tony Andrews
I think it's just because that is what the SQL standard specifies. I don't agree with gd047 that the alias couldn'tbe used: the HAVING clause operates on the (intermediate) results of the query i.e. after aggregation, so it would seem pretty straight-forward for the query parser to use the alias to access that result.
我认为这只是因为这是 SQL 标准指定的内容。我不同意 gd047不能使用别名的观点:HAVING 子句对查询的(中间)结果进行操作,即聚合后,因此查询解析器使用别名来执行操作似乎非常简单访问该结果。
You can of course avoid repeating the SUM like this (using the example from the linked question):
您当然可以避免像这样重复 SUM(使用链接问题中的示例):
SELECT *
from
( SELECT donem, bolge_adi, sehir_tasra "1=S, 2=T",
COUNT(DISTINCT mekankodu) "M.SAYISI",
SUM(b2b_dagitim + b2b_transfer - b2b_iade) satis
FROM mps_view2
WHERE donem IN ('200612','200712','200812','200912')
AND (ob IS NOT NULL OR b2b_ob IS NOT NULL)
GROUP BY donem, bolge_adi, sehir_tasra
)
WHERE satis > 0
ORDER BY donem, bolge_adi, sehir_tasra
回答by George Dontas
Column aliases work only with order by clause, because order by is performed after select and all others before select, so they do not have any idea about alias.
列别名仅适用于 order by 子句,因为 order by 在 select 之后执行,而在 select 之前执行所有其他操作,因此他们对别名没有任何概念。
You can see this page. I believe that Oracle's engine works the same way.
你可以看到这个页面。我相信 Oracle 的引擎也以同样的方式工作。