where字句后面不能使用聚合函数
但是having后面可以使用聚合函数,而且可以使用select查询的聚合函数结果,例如下面查询中,select后面使用聚合函数count(*),给聚合函数查询结果取别名num,那么having后面可以直接使用num这个字段
with t1 as (
select
user_id,
count(*) num
from
order_info
where
date>'2025-10-15'
and product_name in ('C++','Java','Python')
and status='completed'
group by user_id
having num>=2
)
select
user_id
from
t1
order by
user_id

京公网安备 11010502036488号