0%

MySQL操作实例(1)

1-1 操作符混合使用

题目:现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据,取出的数据按照device_id升序排列。

示例:user_profile

id device_id gender age university province gpa
1 2138 male 21 北京大学 BeiJing 3.4
2 3214 male NULL 复旦大学 Shanghai 4
3 6543 female 20 北京大学 BeiJing 3.2
4 2315 female 23 浙江大学 ZheJiang 3.6
5 5432 male 25 山东大学 Shandong 3.8

根据输入,你的查询应返回以下结果:(该题对于小数点后面的0不需要计算与统计,后台系统会统一输出小数点后面1位)

device_id gender age university gpa
3214 male NULL 复旦大学 4
5432 male 25 山东大学 3.8

代码:

1
2
3
4
5
6
select device_id, gender, age, university, gpa
from user_profile
where
(gpa>3.5 and university = '山东大学') or
(gpa>3.8 and university = '复旦大学')
order by device_id;

1-2 计算函数

题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。

示例:user_profile

id device_id gender age university gpa
1 2138 male 21 北京大学 3.4
2 3214 male 复旦大学 4.0
3 6543 female 20 北京大学 3.2
4 2315 female 23 浙江大学 3.6
5 5432 male 25 山东大学 3.8
6 2131 male 28 北京师范大学 3.3

根据输入,你的查询应返回以下结果,结果使用round保留到小数点后面1位

male_num avg_gpa
4 3.6

代码:

1
2
3
4
5
select 
count(*) as male_num,
round(avg(gpa), 1) as avg_gpa
from user_profile
where gender = 'male';

1-3 分组查询

描述

题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

用户信息表:user_profile

30天内活跃天数字段(active_days_within_30)

发帖数量字段(question_cnt)

回答数量字段(answer_cnt)

id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4.0 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 male 26 复旦大学 3.6 9 6 52

你的查询返回结果需要对性别和学校分组,示例如下,结果保留1位小数,1位小数之后的四舍五入,查询出来的结果按照gender、university升序排列:

gender university user_num avg_active_day avg_question_cnt
female 北京大学 1 12.0 3.0
female 浙江大学 1 5.0 1.0
male 北京大学 1 7.0 2.0
male 复旦大学 2 12.0 5.5
male 山东大学 2 17.5 11.0

代码:

1
2
3
4
5
6
7
select gender, university,
count(device_id)as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by gender, university
order by gender #显式排序

1-4 分组过滤

题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。

示例:user_profile

id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4.0 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 female 26 复旦大学 3.6 9 6 52

根据示例,你的查询应返回以下结果,注意返回的字段名需要保持一致,同时保留3位小数(系统后台也会自动校正),3位之后四舍五入:

university avg_question_cnt avg_answer_cnt
北京大学 2.500 21.000
浙江大学 1.000 2.000

代码:

1
2
3
4
5
6
7
8
select 
university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg_question_cnt < 5
or avg_answer_cnt < 20;

备注:

  • where 用于筛选原有字段,having 用于筛选新建字段。

1-5 多表查询

题目:现在运营想要查看所有来自浙江大学的用户题目回答明细情况,请你取出相应数据

示例 :question_practice_detail

id device_id question_id result
1 2138 111 wrong
2 3214 112 wrong
3 3214 113 wrong
4 6543 114 right
5 2315 115 right
6 2315 116 right
7 2315 117 wrong

示例:user_profile

id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4.0 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 female 26 复旦大学 3.6 9 6 52

根据示例,你的查询应返回以下结果,查询结果根据question_id升序排序:

device_id question_id result
2315 115 right
2315 116 right
2315 117 wrong
1
2
3
4
5
6
select
a.device_id, a.question_id, a.result
from question_practice_detail  a
inner join user_profile  b
on a.device_id = b.device_id
where b.university = '浙江大学';