多表查询
子查询
selectdevice_id,question_id,resultfromquestion_practice_detailwheredevice_id = ( select device_id from user_profile where university = '浙江大学')order by question_id asc
select question_practice_detail.device_id, question_id, resultfrom question_practice_detail inner join user_profile on question_practice_detail.device_id = user_profile.device_idwhere user_profile.university = '浙江大学'order by question_id asc
链接查询
统计每个学校的答过题的用户的平均答题数
select u.university, round( count(u.device_id)/count(distinct u.device_id),4 ) avg_answer_cntfrom user_profile u join question_practice_detail q on u.device_id = q.device_idgroup by u.universityorder by u.university
select university, round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cntfrom user_profile u, question_practice_detail pwhere p.device_id = u.device_idgroup by universityorder by university
统计每个学校各难度的用户平均刷题数
select u.university, q2.difficult_level, round(count(u.device_id) / count(distinct u.device_id), 4) avg_answer_cntfrom user_profile u join question_practice_detail q on u.device_id = q.device_id join question_detail q2 on q2.question_id = q.question_idgroup by u.university, q2.difficult_levelorder by u.university
select university, p1.difficult_level, round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cntfrom user_profile u, question_practice_detail p, question_detail p1where p.device_id = u.device_id and p1.question_id = p.question_idgroup by university, p1.difficult_levelorder by university
统计每个用户的平均刷题数
select u.university, q2.difficult_level, round(count(u.device_id) / count(distinct u.device_id), 4) avg_answer_cntfrom user_profile u join question_practice_detail q on u.device_id = q.device_id join question_detail q2 on q2.question_id = q.question_idgroup by u.university, q2.difficult_levelhaving u.university = '山东大学'
select university, p1.difficult_level, round(count(1) / count(distinct(u.device_id)), 4) as avg_answer_cntfrom user_profile u, question_practice_detail p, question_detail p1where p.device_id = u.device_id and p1.question_id = p.question_idgroup by university, p1.difficult_levelhaving university = '山东大学'
组合查询
查找山东大学或者性别为男生的信息
select device_id, gender, age, gpafrom user_profilewhere university = '山东大学'union allselect device_id, gender, age, gpafrom user_profilewhere gender = 'male'
计算25岁以上和以下的用户数量
select (case when age<25 then '25岁以下' when age is null then '25岁以下'else '25岁及以上' end) as age_cut,count(*) numberfrom user_profilegroup by age_cut
select (case when age>=25 then '25岁及以上'else '25岁以下' end) as age_cut,count(*) numberfrom user_profilegroup by age_cut
查看不同年龄段的用户明细
select device_id,gender,(case when age<20 then '20岁以下' when age>=20 and age<25 then '20-24岁' when age>=25 then '25岁及以上'else '其他' end) as age_cutfrom user_profile
日期函数
计算用户8月每天的练题数量
select day(date) as day, count(1) as question_cntfrom question_practice_detailwhere year(date) = 2021 and month(date) = 8group by date
select day(date) as day, count(1) as question_cntfrom question_practice_detailgroup by datehaving ( year(date) = 2021 and month(date) = 8 )