一句话:
group by子句让聚合函数输出不同的记录,having子句优先级低于聚合函数,其就是用于筛选聚合后的各组值,以弥补where子句的不足。所以,having子句中的条件必须是聚合函数。
例子:
update t_userinfo tu set tu.status = 3
, tu.update_time = to_date(str_date, 'yyyy-mm-dd hh24:mi:ss')
where tu.status = 1
and exists (select 1 from t_annoyance ta
where ta.call_userid = tu.userid
and ta.insert_time >= to_date(str_date, 'yyyy-mm-dd hh24:mi:ss') - 30
and ta.call_time < 6
group by tu.userid
having count(distinct ta.becall_userid) > 50)
参考: