• 周六. 4月 27th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

mysql详解4:分组

admin

11月 28, 2021

聚合函数
SUM 求和 COUNT计数
DISTNCT 去重
select COUNT(DISTNCT client_id) as total_records
from invoices where invoice_date>‘2018-04-02’查询并去重

分组
group by
根据多列分组
select state,city,SUM(invoice_total) as total_sales
from invoices
JOIN clients using (client_id)
where invoice_date >=’2019-07-01′
GROUP BY state,city

having 是分组后对数据筛选 where是分组前筛选
having 只能提到选择的列

rollup 汇总 只能用于复合函数
多列复合 会得到整个组的的汇总和各个组的汇总
with rollup 不能使用别名

select client_id,
sum(invoice_total) total
from invoices
GROUP BY client_id with ROLLUP

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注