一、非技术快速入门
https://www.nowcoder.com/exam/oj?page=1&tab=SQL篇&topicId=199
题目记录:
SQL34 统计复旦用户8月练题情况
- 题目
-
结果
-
代码:
select up.device_id, '复旦大学' as university,
count(question_id) as question_cnt,
# 计算做对的题目的个数
sum(if(qpd.result='right', 1, 0)) as right_question_cnt
from user_profile as up
left join question_practice_detail as qpd
on qpd.device_id = up.device_id and month(qpd.date) = 8
where up.university = '复旦大学'
group by up.device_id
问题:如何确定那个表是主表?--看题目中的查询结果是以哪个表为准:因为要未做过题的用户,所以要用user表为主,如果以question表为主的话,那就只有做过的题的用户了。
SQL35 浙大不同难度题目的正确率
- 题目:
-
结果:
-
代码:
select difficult_level,
avg(if(qpd.result='right', 1, 0)) as correct_rate
# sum(if(qpd.result='right', 1, 0)) / count(qpd.question_id) as correct_rate
# count(if(qpd.result='right', 1, null)) / count(qpd.question_id) as correct_rate
from user_profile as up
inner join question_practice_detail as qpd
on up.device_id = qpd.device_id
inner join question_detail as qd
on qd.question_id = qpd.question_id
where up.university = '浙江大学'
group by qd.difficult_level
order by correct_rate asc;
试题分析:1.先写select的部分;2.看筛选的字段与表中哪些字段相关,如果表与表之间有字段相等的关系,就使用关联;3.在where\group by\order by的部分调用各自表信息的限定条件。
注意:多张表联合查询:需要用到join,join有多种语法,因为条件限定需要是浙江大学的用户,所以需要是user_profile表的并且能统计出题目难度的记录,因此用user_profile表inner join另外两张表。
SQL39 21年8月份练题总数
-
题目:
-
结果:
-
代码:
select
count(distinct device_id) as did_cnt,
count(question_id) as question_cnt
from question_practice_detail
where date like '2021-08%'
标签:count,question,up,qpd,牛客,sql,device,id,刷题 From: https://www.cnblogs.com/yuyingblogs/p/17827989.html注意:对用户需要进行去重