您的位置:首页 > 其它

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

2015-11-19 22:32 399 查看
查询每种分类下的商品总数 : 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)字段在 表中没有 所以不能使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: