《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'; #检索条件 ...
随机推荐
- thread库,附带程序介绍
千万注意线程不要比主线程晚结束,主线程一旦结束,对象将被销毁 std:thread 代表了一个线程对象 this_thread:是一个命名空间,对当前调用者线程进行操作 如果把可调用对象作为参数传递给 ...
- 大数java(pow)
Problems involving the computation of exact values of very large magnitude and precision are common. ...
- 百度云的ubuntu16.04.1部署Apache服务器+Django项目
使用Apache和mod_wsgi部署Django 是一种久经考验的将Django投入生产的方法. mod_wsgi是一个Apache模块,可以托管任何Python WSGI应用程序,包括Django ...
- django添加装饰器
引入模块: from django.utils.decorators import method_decorator 添加:@method_decorator(func) from django.ut ...
- [Robot Framework] 搭建Robot Framework和RIDE(Robot Framework GUI) 的环境
在windows x64的环境上进行安装,集成Selenium2和AutoIt的libraries,以下安装步骤在win 7,win 8.1,win 10, win 2012 R2上测试通过 1. 下 ...
- Android中fragment之间和Activity的传值、切换
功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...
- 用php脚本比较MySQL两个数据库的结构差异
define('DATABASE1', 'mysql://root:password@127.0.0.1/db1'); $dbi1 = new DbMysql; $dbi1->dbh = DAT ...
- random库的常见用法
import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 print( random.random() ) # 产生 0 到 ...
- 【Web】Sublime Text 3 连接sftp/ftp(远程服务器)
在 Win 下常用 Xftp 软件来和远程服务传递文件,但是要是在项目开发的时候频繁的将远程文件拖到本地编辑然后再传回远程服务器,那真是麻烦无比,但是Sublime中SFTP插件,它让这世界美好了许多 ...
- Likecloud—吃、吃、吃(P1508)
题目链接:Likecloud-吃.吃.吃 这题的状态非常的自然. 就是ans[i][j]表示从(i,j)出发,能得到的最大能量值. 那么对应每一个点,我们只要选出他能到达的点的最大值,加上自己就行了. ...