《mysql必知必会》学习_第13章_20180803_欢
第13章:分组过滤。
P83
select count(*) as num_prods from products where vend_id=1003; #返回vend_id=1003的产品数目总值#
P84
select count(*) as num_prods from products group by vend_id; #返回各个vend_id所提供的产品数量,不需要指定计算每个组,因为用了group by ,系统会自动完成#
注意:group by必须在where 之后,order by 之前(当然是在需要where 或order by 的情况下)
P85
select vend_id,count(*) as num_prods from products group by vend_id with rollup ; # 使用with rollup关键字,可以得到每个分组以及每个分组汇总级别(针对每个分组)的值###其实我觉得使用了with rollup与否,对结果并没有影响,来个可爱告诉我使用不使用的区别吧###
P85 过滤分组 -- having语句
select cust_id,count(*) as orders from orders group by cust_id having count(*)>=2l; #where 过滤行,having过滤分组###我理解的是where对表初始的行数据过滤,而having是聚集函数处理以后的分组过滤(感觉不大对)#####对面大神说用了group by了不能用where,要用having。(P87页说和聚集函数一起使用列,必须使用group by,不能使用order by ,相应地,下面的csdn的网友说,聚集函数要用having筛选分组,所以以此类推,用了group by不能用where,要用having的意思吗??? )###
P86
select vend_id,count(*) as num_prods from products where prod_price>=10 group by vend_id having count(*)>=2; #条件是列prod_price>=10,count(*)>=2 ,这个语句同时出现where和having #
有个我认为不错的解释:https://blog.csdn.net/zqtsx/article/details/41869049
P88
select order_num,sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(quantity*item_price)>=50; #group by 让排列按order_num来排列,having设定了组sum(quantity*item_price)或者是ordertotal大于50的条件.
select order_num,sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(quantity*item_price)>=50 order by ordertotal ; #group by 让排列按order_num来排列,having设定了聚集函数得到的组sum(quantity*item_price)大于等于50,order by 得到最后的顺序由ordertotal排序输出(ordertatal的顺序默认是升序)#.
#group by 分组说明,仅限于聚集函数得到的分组;
having :组级过滤;
where:行级过滤,表中的行;
order by :输出的排序过滤。
limit n :要检索的行数;
limit (n,m) :要检索的(开始的行数:第n个开始,检索m行即是第n+m个结束)#
最新文章
- BZOJ3160万径人踪灭
- wcf第3步之报文分析及原生调用
- Python类的实例属性详解
- 转:DataTable的Compute方法的应用
- sort如何按指定的列排序
- time模块学习
- HDU(3605),二分图多重匹配
- Codeforces Round #283 (Div. 2)
- KM算法(二分图的最佳完美匹配)
- 接口作为参数,不同的接口调用不同的方法,例如:输出“I love Game”或输出“我喜欢游戏”
- sql 学习笔记 档
- Java :BufferedWriter类和BufferedReader类的构造方法、主要方法
- JavaScript+HTML5 实现打地鼠小游戏
- setTimeout与setInterval
- linux setup的安装
- 用kubernetes部署oa 强制删除pod delete
- (转)Java随机数
- 分享10款效果惊艳的HTML5图片特效【转】
- plsql的sql窗口中文模糊查询没有作用
- 层层递进Struts1(三)之Struts组成