第六章_查询 【where、having 的区别】
1. where 和 having的区别
1. 使用位置
1. where 只能在group by 之前使用,且不能对 分组函数做限制
2. having 只能在 group by 之后使用,可以对 分组维度字段, 和分组函数 做限制
2. 使用别名
1. where 不能使用别名
2. having 可以使用别名
2. 示例
with t1 as ( select '张飞' as name,1 as num union all select '张飞' as name,1 as num union all select '刘备' as name,1 as num union all select '赵云' as name,1 as num union all select '关羽' as name,1 as num union all select '黄忠' as name,1 as num) select name ,count(num) as cnt from t1 group by name having cnt = 2