为什么我不能在count(*)“ column”列中使用别名?并在having子句中引用它? [英] Why can't I use alias in a count(*) "column" and reference it in a having clause?

查看:207
本文介绍了为什么我不能在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 文档 :// answer 到您最近的问题

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.



  1. 首先形成 from 子句中所有表的乘积。

  2. 然后评估 where 子句以消除行不能满足
    的搜索条件。

  3. 接下来,使用 group by 子句中的列对行进行分组。
  4. 然后,具有不满足search_condition的组删除
    子句。

  5. 接下来,对 select 子句目标列表中的表达式进行
    求值。

  6. 如果select子句中存在 distinct 关键字,则现在将消除重复的行

  7. > un 是在评估每个子选择之后获取的。

  8. 最后,结果行将根据顺序中指定的
    列进行排序
    子句。

  1. First the product of all tables in the from clause is formed.
  2. The where clause is then evaluated to eliminate rows that do not satisfy the search_condition.
  3. Next, the rows are grouped using the columns in the group by clause.
  4. Then, Groups that do not satisfy the search_condition in the having clause are eliminated.
  5. Next, the expressions in the select clause target list are evaluated.
  6. If the distinct keyword in present in the select clause, duplicate rows are now eliminated.
  7. The union is taken after each sub-select is evaluated.
  8. Finally, the resulting rows are sorted according to the columns specified in the order by clause.


这篇关于为什么我不能在count(*)“ column”列中使用别名?并在having子句中引用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆