首页 > 数据库 > mysql教程 > mysql having

mysql having

黄舟
发布: 2017-01-16 13:10:38
原创
1903 人浏览过

having
查询差价在200以上的列

select goods_id,(market_price - shop_price ) as chajia from goods having chajia>200;
登录后复制

查询挤压的总货款

select sum(goods_number*shop_price) from goods;
登录后复制

查询每个栏目下的积压货款

mysql> select cat_id ,sum(goods_number*shop_price) from goods group by cat_id;
+--------+------------------------------+
| cat_id | sum(goods_number*shop_price) |
+--------+------------------------------+
| 2 | 0.00 | 
| 3 | 356235.00 | 
| 4 | 9891.00 | 
| 5 | 29600.00 | 
| 8 | 4618.00 | 
| 11 | 790.00 | 
| 13 | 134.00 | 
| 14 | 162.00 | 
| 15 | 190.00 | 
+--------+------------------------------+
登录后复制

查询积压货款大于20000的栏目

mysql> select cat_id ,(sum(goods_number*shop_price)) as dae from goods group by cat_id having dae > 20000;
+--------+-----------+
| cat_id | dae |
+--------+-----------+
| 3 | 356235.00 | 
| 5 | 29600.00 | 
+--------+-----------+
insert into result
values
('张三','数学',90),
('张三','语文',50),
('张三','地理',40),
('李四','语文',55),
('李四','政治',45),
('王五','政治',30);
登录后复制

求出两门以上不及格人的平均值

逆向逻辑

select name,avg(score) from result group by name having (sum(score<60))>=2 ;
登录后复制

两者等同

select name,avg(score),sum(score<60) as guake from result group by name having guake>=2;
登录后复制

正向逻辑 (用到了子查询)

select name,avg(score)
from result
where name in ( 
select name from ( 
(select name ,count(*) as guake from result where score<60 group by name having guake>=2) as tmp 
)
)
group by name;
登录后复制

以上就是mysql having的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板