为什么我不能在count(*)“ column”列中使用别名?并在having子句中引用它? [英] Why can't I use alias in a count(*) "column" and reference it in a having clause?
本文介绍了为什么我不能在count(*)“ column”列中使用别名?并在having子句中引用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么我不能在count(*)中使用别名并在hading子句中引用它。例如:
I was wondering why can't I use alias in a count(*) and reference it in the having clause. For instance:
select Store_id as StoreId, count(*) as _count
from StoreProduct
group by Store_id
having _count > 0
不起作用。但是如果我删除_count并使用count(*)代替,它将起作用。
Wouldn't work.. But it works if I remove _count and use count(*) instead.
推荐答案
请参见 CodeByMoonlight 在
See the document referenced by CodeByMoonlight in an answer to your recent question.
在SELECT之前评估HAVING子句-因此服务器尚不知道该别名。
The HAVING clause is evaluated before the SELECT - so the server doesn't yet know about that alias.
- 首先形成 from 子句中所有表的乘积。
- 然后评估 where 子句以消除行不能满足
的搜索条件。 - 然后,具有不满足search_condition的组删除
子句。 - 接下来,对 select 子句目标列表中的表达式进行
求值。 - 如果select子句中存在 distinct 关键字,则现在将消除重复的行
。 - > un 是在评估每个子选择之后获取的。
- 最后,结果行将根据顺序中指定的
列进行排序子句。
- First the product of all tables in the from clause is formed.
- The where clause is then evaluated to eliminate rows that do not satisfy the search_condition.
- Next, the rows are grouped using the columns in the group by clause.
- Then, Groups that do not satisfy the search_condition in the having clause are eliminated.
- Next, the expressions in the select clause target list are evaluated.
- If the distinct keyword in present in the select clause, duplicate rows are now eliminated.
- The union is taken after each sub-select is evaluated.
- Finally, the resulting rows are sorted according to the columns specified in the order by clause.
这篇关于为什么我不能在count(*)“ column”列中使用别名?并在having子句中引用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文