mysql having
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;
轉載于:https://blog.51cto.com/codeyi/1732461
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的mysql having的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 2195 Going Home
- 下一篇: linux cmake编译源码,linu