having 与where 的异同点
having 与where 的异同点: where针对表中的列发挥作用,查询数据 having对查询结果中的列发挥作用,筛选数据
#查询本店商品价格比市场价低多少钱,输出低200元以上的商品 select goods_id, market_price - shop_price as s from goods having s>200 ; //这里不能用where因为s是查询结果,而where只能对表中的字段名筛选
如果用where的话则是: select goods_id,goods_name from goods where market_price - shop_price > 200;
#同时使用where与having select cat_id, market_price - shop_price as s from goods where cat_id = 3 having s > 200;
#查询积压货款超过2万元的栏目,以及该栏目积压的货款 select cat_id, sum(shop_price * goods_number) as t from goods group by cat_id having s > 20000
#查询两门及两门以上科目不及格的学生的平均分 思路: #先计算所有学生的平均分 select name, avg(score) as pj from stu group by name; #查出所有学生的挂科情况 select name,score<60 from stu; #这里score<60是判断语句,所以结果为真或假,mysql中真为1假为0 #查出两门及两门以上不及格的学生 select name, sum(score<60) as gk from stu group by name having gk > 1; #综合结果 select name,sum(score<60) as gk, avg(score) as pj from stu group by name having gk >1;
无语言基础,自学python所做的各种笔记,欢迎大牛指点.