From 669136154c38653c97d4093d081f50528b349fed Mon Sep 17 00:00:00 2001 From: smallkun Date: Thu, 27 Feb 2025 10:34:54 +0800 Subject: [PATCH] Auto commit --- 2207/MySQL练习题/查询练习.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/2207/MySQL练习题/查询练习.md b/2207/MySQL练习题/查询练习.md index 0719e0c..77ae6a4 100644 --- a/2207/MySQL练习题/查询练习.md +++ b/2207/MySQL练习题/查询练习.md @@ -407,16 +407,45 @@ FROM ( )s3; #19.查询每门课程被选修的学生数 - +SELECT c.`name`, COUNT(*) +FROM courses c +LEFT JOIN scores sc ON sc.course_id = c.id +GROUP BY c.id; #20.查询出只选修两门课程的学生学号和姓名 +SELECT st.id, st.`name` +FROM students st +WHERE ( + SELECT COUNT(*) + FROM scores sc + WHERE sc.student_id = st.id +)=2; #21.查询男生、女生人数 +SELECT st.gender, COUNT(*) +FROM students st +GROUP BY st.gender; + +SELECT COUNT(IF(st.gender='男', TRUE, NULL)) '男生人数', +COUNT(IF(st.gender='女', TRUE, NULL)) '女生人数' +FROM students st; #22.查询名字中含有「风」字的学生信息 +SELECT * +FROM students st +WHERE st.`name` LIKE '%风%'; + #23.查询同名同性学生名单,并统计同名人数 +SELECT s.`name`, s.gender, COUNT(*) +FROM students s +GROUP BY s.`name`, s.gender +HAVING COUNT(*) > 1; + #24.查询 1990 年出生的学生名单 +SELECT * +FROM students s +WHERE YEAR(s.birth) = 1990; #25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列