Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > mysql聚合函数、分组查询、时间函数

MySQL 聚合函数、分组查询、时间函数详解

作者:不止萩

这篇文章主要介绍了MySQL 聚合函数、分组查询、时间函数 ,主要考验聚合函数count()求和以及分组查询,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

一、题目:每位教师所教授的科目种类的数量

本题主要考验聚合函数count()求和以及分组查询。

1 准备工作

Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int);
Truncate table Teacher;
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '3');
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '4');
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '3', '3');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '1', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '2', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '3', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '4', '1');

查询每位老师在大学里教授的科目种类的数量;

示例 1:

输入:

Teacher 表:

输出: 

2 分析

3 实现 

select
    activity_date day,
    count(distinct user_id) active_users
from activity
where datediff('2019-07-27',activity_date) between 0 and 29
group by activity_date;

二 、查询近30天活跃用户数

本题主要考验

1 准备工作

Create table If Not Exists Activity (user_id int, session_id int, activity_date date, activity_type ENUM('open_session', 'end_session', 'scroll_down', 'send_message'));
Truncate table Activity;
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'open_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'scroll_down');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('1', '1', '2019-07-20', 'end_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-20', 'open_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'send_message');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('2', '4', '2019-07-21', 'end_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'open_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'send_message');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('3', '2', '2019-07-21', 'end_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'open_session');
insert into Activity (user_id, session_id, activity_date, activity_type) values ('4', '3', '2019-06-25', 'end_session');

统计截至 2019-07-27(包含07-27),近30天的每日活跃用户数(当天只要有一条活动记录,即为活跃用户)

示例 2 :

输入:

输出:

2 分析 

首先需要查询的字段为时间和用户登录数,对时间分组并且对用户人数统计查询出每日活跃的用户。(含重复id)

对用户的id进行去重查出每日活跃用户

3 实现 

select
    activity_date day,
    count(distinct user_id) active_users
from activity
where datediff('2019-07-27',activity_date) between 0 and 29
group by activity_date;

到此这篇关于MySQL 聚合函数、分组查询、时间函数 的文章就介绍到这了,更多相关mysql聚合函数、分组查询、时间函数 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文