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)字段在 表中没有 所以不能使用
查询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)字段在 表中没有 所以不能使用
相关文章推荐
- win7的系统怎么把屏幕颜色设置成保护眼睛的绿色???
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 博弈
- display:none和visibility:hidden的区别
- bootchartd简介
- java 序列化 serialVersionUID transient
- 【Leetcode】Palindrome Linked List
- AndroidStudio中方法注释模板快捷键的设置
- Android四大组件之Service
- Python __getattr__与__setattr__使用方法
- 第一个Web前端
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- Group Anagrams
- PAT-PAT (Advanced Level) Practise To Buy or Not to Buy(20) 【一星级】
- EventBroker
- Xcode7.0后,如何配置Pch
- 13.按比例显示图片、自定义属性、测量
- 百度api定位
- gradle eclipse下构建ssh2项目
- ofbiz框架以及核心原理介绍
- bzoj-2555 SubString