《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'; #检索条件 ...
随机推荐
- TCP Nagle算法&&延迟确认机制
TCP Nagle算法&&延迟确认机制 收藏 秋风醉了 发表于 3年前 阅读 1367 收藏 0 点赞 0 评论 0 [腾讯云]买域名送云解析+SSL证书+建站!>>> ...
- mac/linux 修改vim显示信息
转自:http://www.cnblogs.com/yjmyzz/p/4019783.html 步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个 ...
- Telnet远程登录
假设 电脑A Telnet远程登录 电脑B (Windows) 1.电脑B: 关闭防火墙 开启Telnet服务:“我的电脑”-->“管理”-->“服务”-->Telnet开启 2.电 ...
- 常用jvm参数
如果你是Eclipse ,可以通过 run -> Run Configurations->Arguments 添加-XX:+PrintGCDetails 打开gc日志 -Xmx 设置jav ...
- 利用PHP脚本辅助MySQL数据库管理1-表结构
<?php $dbi = new DbMysql; $dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array( ...
- oracle创建、删除 数据库、建立表空间以及插入 删除 修改表
一.创建.删除数据库 oracle OraDb11g_home->配置和移植工具->Database configration Assistant->...然后可以创建或者删除数据 ...
- mybatis学习一 环境搭建
第一步导入Jar包 第二步配置sqlMapConfig.xml文件,xml文件放在src下面 <?xml version="1.0" encoding="UTF-8 ...
- liunx基础命令
linux的简单介绍 linux是一款免费使用和自由传播的内似于unix的操作系统软件,是一个基于POSI和unix的多用户,多任务,支持多线程和多CPU的一种操作系统.主要用于服务器,特别是网络服务 ...
- thymeleaf使用基础教程
thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎. thymeleaf介绍 简单说, Thymeleaf 是一个跟 Velocity.FreeM ...
- 2019.01.24 bzoj3125: CITY(轮廓线dp)
传送门 题意简述:给一个n∗mn*mn∗m的网格图,有的格子不能走,有的格子只能竖着走,有的格子只能横着走,问用一条回路覆盖所有能走的格子的方案数. 思路: 就是简单的轮廓线dpdpdp加了一点限制而 ...