1280. 学生们参加各科测试的次数
思路
1.第一个表统计每个学生对应的考试科目
2.第二个表在examinations中,统计每个id对应的考试次数
3.将这两个表连接起来
细节
1.注意第二个表记得命名
2.使用ifnull来处理,学生没考试的那些科目
3.使用count,一定要记得group
# Write your MySQL query statement below
#首先第一个表是学生和对应的考试科目
select s.student_id, s.student_name, sub.subject_name, ifnull(grouped.ad,0) attended_exams
from students s
cross join #交叉连接
subjects sub
#-----------到这行是第一个表建立完------------
#第二个表 统计每个人的考试科目次数
left join(
select student_id,subject_name ,count(*) ad # count要对应group
from examinations
group by student_id,subject_name
)grouped #记得给表命名
on s.student_id=grouped.student_id and sub.subject_name=grouped.subject_name #两个表的连接条件
order by student_id,subject_name #最后的排序规则
posted on 2025-04-28 22:16 swj2529411658 阅读(25) 评论(0) 收藏 举报
浙公网安备 33010602011771号