having可以显示sql语句查询的条件,与where使用类似,在某些场合可以互换使用
where:设置sql语句的查询条件,条件字段必须是 数据表存在的字段
having:设置sql语句的查询条件,条件字段必须是 查询结果集的字段。
① having和where可以互换使用
select * from sw_goods where goods_price> 5000
select * from sw_goods having goods_price> 5000
② 只能使用where(不能用having)
select goods_id,goods_name from sw_goods where goods_price>10000
select goods_id,goods_name from sw_goods having goods_price>10000 错误
③ 只能使用having (不能用where)
--获得总数量大于5个结果的品牌信息
--select goods_brand_id,count(*) fromsw_goods group by goods_brand_id
select goods_brand_id,count(*) as cnt fromsw_goods group by goods_brand_id having cnt>5
select goods_brand_id,count(*) as cnt fromsw_goods where cnt>5 group by goods_brand_id 错误
版权声明:本文为博主原创文章,未经博主允许不得转载。