前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第十课 分组数据创建分组过滤分组分组和排序

第十课 分组数据创建分组过滤分组分组和排序

作者头像
desperate633
发布2018-08-22 10:40:07
1.3K0
发布2018-08-22 10:40:07
举报
文章被收录于专栏:desperate633desperate633

创建分组

代码语言:javascript
复制
select vend_id, count(*) as num_prods
from products
group by vend_id;

group by 语句的规定:

  • 可以包含任意数目的列,因而可以对分组进行嵌套
  • 必须出现在where语句之后,having语句之前 等等

过滤分组

过滤掉不符合条件的分组,使用having而不是where

** having和where的区别 **: ** where在数据分组前进行过滤,having在数据分组后进行过滤,where过滤的是行,having过滤的是分组 **

代码语言:javascript
复制
select cust_id, count(*) as orders
from orders
group by cust_id
having count(*) >= 2;

select vend_id, count(*) as num_prods
from products
where prod_price >= 4
group by vend_id
having count(*) >= 2;

分组和排序

Paste_Image.png

代码语言:javascript
复制
select order_num,count(*) as items
from orderItems
group by order_num
having count(*) >= 3
order by items, order_num;

Paste_Image.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.01.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建分组
  • 过滤分组
  • 分组和排序
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档