gpt4 book ai didi

sql - 为什么我不能在 count(*) "column"中使用别名并在having 子句中引用它?

转载 作者:行者123 更新时间:2023-12-01 17:44:25 25 4
gpt4 key购买 nike

我想知道为什么不能在 count(*) 中使用别名并在having 子句中引用它。例如:

select Store_id as StoreId, count(*) as _count
from StoreProduct
group by Store_id
having _count > 0

不起作用..但如果我删除 _count 并使用 count(*) 代替,它就会起作用。

最佳答案

请参阅document referenced通过 CodeByMoonlightansweryour recent question .

HAVING 子句在 SELECT 之前计算 - 因此服务器尚不知道该别名。

  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.

关于sql - 为什么我不能在 count(*) "column"中使用别名并在having 子句中引用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com