having能对结果集进一步筛选,得到最终结果
1、查询本店价格比市场省的钱,并且省的钱都在200以上
select goods_name,cast(market_price as signed)-cast(shop_price as signed) as discount from goods having discount >200;
2、查询每个商品所积压的货款
select goods_name,shop_price*goods_number as hk from goods ;
3、查询该店积压的总货款
select sum(shop_price*goods_number) from goods;
4、查询每个栏目下积压的货款
select cat_id,sum(shop_price*goods_number) from goods group by cat_id;
5、查询积压货款超过2000的栏目,以及该栏目积压的货款
(分组后筛选)
select cat_id,sum(shop_price*goods_number) as hk from goods group by cat_id having hk>20000;
6、查询比市场价省钱200元以上的商品及该商品所省的钱
select goods_name,cast(market_price as signed)-cast(shop_price as signed) as discount from goods having discount>200;
7、查询如下表格 :两门或两门以上不及格者的平均成绩
方法一:
select name,avg(score),sum(score<60) as gks from result group by name having gks>=2;
方法二:子查询
select name,avg(score) from result where name in(select name from(select name,count(1) as gks from result where score<60 group by name having gks>=2 ) as tmp) group by name;