MySQL 聚合函数、分组查询、时间函数详解
一、题目:每位教师所教授的科目种类的数量
本题主要考验聚合函数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天活跃用户数
本题主要考验
分组查询
字符串函数count统计人数
时间函数定义时间
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聚合函数、分组查询、时间函数 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
mysql-5.7.42升级到mysql-8.2.0(二进制方式)
随着数据量的增长和业务需求的变更,我们可能需要升级MySQL,本文主要介绍了mysql-5.7.42升级到mysql-8.2.0(二进制方式),具有一定的参考价值,感兴趣的可以了解一下2024-03-03MySQL SQL性能分析之慢查询日志、explain使用详解
这篇文章主要介绍了MySQL SQL性能分析 慢查询日志、explain使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-04-04
最新评论