group 分组 查询 栏目下文章 的总数 having where区别

查询每种分类下的商品总数 : select cate_id count(*) from sw_goods group by cate_id    获取栏目名称 连表查询

查询cate_id=2的商品总数 : select cate_id count(*) from sw_goods where cate_id=2  group by cate_id   获取文章总数 连表查询

查询 cate_id = 2 的 商品的 价格总和 : select sum(price) from sw_goods where cate_id=2 group by cate_id    获取栏目名称 连表查询


having where 区别

1 where 在前  having 在后 

2 where 的条件字段 必须在原来的表中存在该字段 才能使用   having 使用的条件必须是 通过select 查询的 字段 才能 having

select price ,name,address from goods where price>100  正确

select price ,name,address from goods having price>100  正确  选择了price  

select price ,name,address from goods having weight>100  错误  选择了weight 但是没有查询weight 错误  但是用  where 是可以的 


查询商品平均价格大于1000的所有的商品种类 

select  name avg(price) from goods group by cate_id  having avg(price)>1000  这种情况只能使用having  他是现算出来的字段 只能使用 having  不能使用where 

select  name avg(price) from goods where   avg(price>1000  group by cate_id   错误  这个avg(price)字段在 表中没有 所以不能使用 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值