1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | -- select s_id from student; -- select c_id from course where c_name = 'python' -- SELECT s_id,num from score where c_id in(select c_id from course where c_name = 'python') and s_id in(select s_id from student); -- SELECT s_id,num from score where c_id in(select c_id from course where c_name = 'java') and s_id in(select s_id from student) #查询学习课程 "python" 比课程 "java" 成绩高的学生的学号; -- SELECT id from (SELECT s_id as id,num from score where c_id in(select c_id from course where c_name = 'python') <br>and s_id in(select s_id from student)) as p1 -- INNER JOIN <br>(SELECT s_id,num from score where c_id in(select c_id from course where c_name = 'java') <br>and s_id in(select s_id from student)) as p2 on p1.id = p2.s_id -- where p1.num > p2.num; #查询平均成绩大于65分的同学的姓名和平均成绩(保留两位小数); #ROUND(n,小数的位数) -- SELECT avg(num) from score GROUP BY s_id ; -- select s_name,ROUND(mid,2) from student s LEFT JOIN (SELECT avg(num)as mid,s_id from score GROUP BY s_id -- ) as p on s.s_id = p.s_id where mid >65; #查询所有同学的姓名、选课数、总成绩; -- select s_name,c_num,s_sum from -- (select s_id,sum(num)as s_sum,count(c_id) as c_num from score GROUP BY s_id) as t -- LEFT JOIN student on t.s_id = student.s_id #查询所有的课程的名称以及对应的任课老师姓名; -- select c_name,t_name from -- (select c_name,t_id from course) as t LEFT JOIN teacher on t.t_id = teacher.t_id #查询没学过“alex”老师课的同学的姓名; #先查学过的,最后在否定一下。 -- select s_name from student where s_id not in -- (SELECT s_id from score where c_id in (SELECT c_id from course where t_id in(select t_id from teacher where t_name = 'alex'))) # in --or #查询挂科超过两门(包括两门)的学生姓名 -- select s_name from student as s LEFT JOIN -- (select s.s_id,sum(case WHEN s.num<= 60 THEN 1 ELSE 0 end) as number -- from score as s GROUP BY s.s_id) as n on s.s_id = n.s_id WHERE number >=2 #查询学过 'python' 并且也学过编号 'java' 课程的同学的姓名; -- select score.s_id,count(*)as number from course,score where course.c_id = score.c_id and course.c_name in ('python','java') <br>GROUP BY score.s_id HAVING number >=2 #在没有分组的时候的行数就是 count (*),就是 count (*)相当于行数,但是不能都表示出来,如果要表示就要GROUP_CONCAT -- SELECT student.s_name from student, -- (select score.s_id,count(*)as number from course,score where course.c_id = score.c_id and course.c_name in ('python','java') <br>GROUP BY score.s_id HAVING number >=2) as s where student.s_id = s.s_id #查询学过 '貂蝉' 同学全部课程 的其他同学姓名; -- SELECT s_id,count(c_id) FROM score WHERE c_id in -- (SELECT n.c_id from student as s,score as n WHERE s.s_id =n.s_id and s.s_name = '貂蝉') <br>GROUP BY s_id HAVING count(c_id)>=2 and s_id!= (SELECT s_id from student where s_name = '貂蝉') #用 count (*) -- select student.s_name from student,score where student.s_id = score.s_id and score.c_id in -- (select c_id from student,score where student.s_id = score.s_id and student.s_name = '貂蝉') and student.s_name !='貂蝉'<br>GROUP BY student.s_id -- HAVING count(*) >= (select count(*) from student,score where student.s_id = score.s_id and student.s_name = '貂蝉') #查询和 '貂蝉' 同学学习的课程完全相同的,其他同学姓名; -- SELECT student.s_name from student, -- (SELECT score.s_id, -- sum(case when c_id in <br>(SELECT n.c_id from student as s,score as n WHERE s.s_id =n.s_id and s.s_name = '貂蝉')THEN 1 ELSE -1 END ) AS N -- from score where score.s_id !=(SELECT student.s_id from student where s_name = '貂蝉') GROUP BY score.s_id HAVING N=2) as m -- where student.s_id = m.s_id #先通过貂蝉的科目数 筛选 与貂蝉选相同科目的人 ,然后通过具体的科目来筛选; ## count (*)相当于没有分组前的行数 # SELECT student.s_id from student,score where student.s_id = score.s_id GROUP BY score.s_id HAVING count (*)= #( SELECT count (*) FROM student,score where student.s_id = score.s_id and student.s_name = '貂蝉' ) ---(2,5) -- SELECT DISTINCT student.s_name from student,score where student.s_id = score.s_id and score.s_id in (2,5) AND score.c_id in -- (SELECT score.c_id FROM student,score where student.s_id = score.s_id and student.s_name = '貂蝉') and student.s_name != '貂蝉' #按平均成绩倒序显示所有学生的“python”、“java”、“linux”三门的课程成绩,按如下形式显示: 学生ID,python,java,linux,课程数,平均分 -- SELECT num from score,course where score.c_id = course.c_id and course.c_name ='python' and score.s_id = 1 #一个数据就可以作为一个字段,即把一个结果集作为一个字段; -- SELECT s.s_id as '学生ID', -- (SELECT num from score,course where score.c_id = course.c_id and course.c_name ='python' and score.s_id = s.s_id) as 'python', -- (SELECT num from score,course where score.c_id = course.c_id and course.c_name ='java' and score.s_id = s.s_id) as 'java', -- (SELECT num from score,course where score.c_id = course.c_id and course.c_name ='linux' and score.s_id = s.s_id) as 'linux', -- count(*) as '课程数', -- ROUND(avg(s.num),2) as '平均分' -- from score as s GROUP BY s.s_id ORDER BY '平均分' DESC -- #查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 -- SELECT course.c_name,avg(num) FROM score,course where score.c_id = course.c_id <br>GROUP BY score.c_id ORDER BY avg(num) ASC , score.c_id DESC #按条件排序,可以按照多个条件,如果第一个条件不满足,则按照第二个条件;即 平均成绩相同时,按课程号降序排列 |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步