笔记-多表查询好代码比较
统计每个用户的平均刷题数
https://www.nowcoder.com/practice/f4714f7529404679b7f8909c96299ac4?tpId=199&tags=&title=&difficulty=0&judgeStatus=0&rp=0
多表查询一些好的代码比较,上方为案例好代码,下方为自己的代码
SELECT
t1.university,
t3.difficult_level,
COUNT(t2.question_id) / COUNT(DISTINCT(t2.device_id)) as avg_answer_cnt
from
user_profile as t1,
question_practice_detail as t2,
question_detail as t3
WHERE
t1.university = '山东大学'
and t1.device_id = t2.device_id
and t2.question_id = t3.question_id
GROUP BY
t3.difficult_level;
上面select部分还是和我的select部分选出abc的某个字段一样,但是就在where部分
把需要关联=的条件写在这里,使得逻辑比较清晰,不像我需要innner来inner去
SELECT
a.university,
c.difficult_level,
COUNT(b.device_id)/COUNT(DISTINCT b.device_id)平均答题数目
FROM
user_profile a
INNER JOIN question_practice_detail b on a.device_id=b.device_id
INNER JOIN question_detail c ON b.question_id =c.question_id
WHERE
a.university='山东大学'
GROUP BY
c.difficult_level