《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个结束)#
《mysql必知必会》学习_第13章_20180803_欢的更多相关文章
- 《mysql必知必会》学习_第18章_20180807_欢
第18章 全文本搜索 P121 #创建一个新表,对表的列进行定义,定义之后,MySQL自动维护该索引# create table productnotes ( note_id int NOT ...
- 《mysql必知必会》学习_第五章_20180730_欢
使用的工具是wamp的Mysql. P29 select prod_name from products; #在表products中选列prod_name,顺寻不是纯粹的随机,但是没有说明排列顺序, ...
- 《mysql必知必会》学习_第22章_20180809_欢
第22章:使用视图,视图是虚拟的表,以表形式呈现的是你查询的结果.并不是说在数据库里面真的存在这个表,但是是真的存在这些数据. select cust_name,cust_contact from c ...
- 《mysql必知必会》学习_第20章_20180809_欢
第20章:更新和删除数据 P140 update customers set_emails='elmer@fudd.com' where cust_id=10005; 更新多个列,用逗号隔开.注意被指 ...
- 《mysql必知必会》学习_第19章_20180809_欢
第19章 插入数据 P132 insert into customers VALUES(NULL,'Pep E.Lapew','100 Main Street',,Los Angeles','CA', ...
- 《mysql必知必会》学习_第17章_20180807_欢
第17章:组合查询 P114 select vend_id ,prod_id,prod_price from products where prod_price <=5 ; select ven ...
- 《mysql必知必会》学习_第16章_20180807_欢
第16章:创建高级联结. P106 select concat(RTrim(vend_name),'(',RTrim(vend_country),')') as vend_title from ven ...
- 《mysql必知必会》学习_第15章_20180806_欢
第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...
- 《mysql必知必会》学习_第14章_20180806_欢
第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2'; #检索条件 ...
随机推荐
- css 需要阴影的效果
box-shadow: 0 0 10px 10px #b9bcbf; CSS3 box-shadow 属性 CSS 参考手册 实例 向 div 元素添加 box-shadow: div { box-s ...
- pycharm创建新django app
Tools -> Run manage.py task -> startapp appName(你的App名称) 或者 optin + R -> startapp appName( ...
- JSP 9个内置对象
JSP内置对象(隐式对象)是JSP容器为每个页面自动实例化的一组对象,开发者可直接使用,也被称为预定义变量. JSP容器提供了9个内置对象 request // javax.servlet.http. ...
- Python之路番外(第二篇):PYTHON基本数据类型和小知识点
一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...
- Win 10 关闭了.net framework 3.5后再开启不成功
背景: Win10 想要使用IETester来测试下页面的浏览器兼容性.在“控制面板”里关闭了.net framework 3.5,再重新开启. 问题: 在重新开启的时候,提示不成功.错误代码0x80 ...
- android 打开新窗口
ImageView loginBtn = (ImageView)findViewById(R.id.login_button); loginBtn.setOnClickListener(new Vie ...
- mybatis 一次执行多条语句
现在的一些互联网应用 为了提高性能,现在一般比较少的使用外键.不是不用,只是在创建数据库不标明外键关系,用程序去维护. 为了维护数据一致性,我们需要手动完成相关数据的删除 比如用户和用户的关注 当用户 ...
- Armadillo installation
1.dependencies sudo apt-get install libopenblas-devsudo apt-get install liblapack-devsudo apt-get in ...
- Flex 确定弹出窗口的绝对位置x , y
var selectbox:Selectbox; selectbox = new SelectBox(); var pt:Point = new Point(0,0); pt = this.paren ...
- spring学习 十七 scope属性,单例多例
Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...