分组数据

16次阅读

共计 261 个字符,预计需要花费 1 分钟才能阅读完成。

1 介绍

分组返回数据,不是 where 的那种返回特定数据

2 创建分组 (group by)

select vend_id, count(*) as num_prods from products group by vend_id;

2.1 分析

按 vend_id 字段分组,然后统计总数返回数据

3 过滤分组 (having)

select cust_id, count(*) as orders 
from orders
group by cust_id
having count(*) >= 2;

3.1 分析

筛选出需要数据

3.2 where 和 having 区别

where 过滤行,having 过滤分组

正文完
 0