我想知道为什么不能在 count(*) 中使用别名并在having 子句中引用它。例如:
select Store_id as StoreId, count(*) as _count
from StoreProduct
group by Store_id
having _count > 0
不起作用..但如果我删除 _count 并使用 count(*) 代替,它就会起作用。
最佳答案
请参阅document referenced通过 CodeByMoonlight在 answer至 your recent question .
HAVING 子句在 SELECT 之前计算 - 因此服务器尚不知道该别名。
- 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.
关于sql - 为什么我不能在 count(*) "column"中使用别名并在having 子句中引用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068682/