mysql 某些作业整理

05-28作业:
尝试外键约束,分别使用三种效果尝试删除和更新操作
重点是cascade restrict(no action) set null
CREATE TABLE stu(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20) NOT NULL,
sex CHAR(2) DEFAULT ‘男’,
phone CHAR(11) UNIQUE NOT NULL,
cid INT NOT NULL,
CONSTRAINT wai FOREIGN KEY(cid)
REFERENCES class(id) ON UPDATE CASCADE
ON DELETE CASCADE
)
在这里插入图片描述
在这里插入图片描述
1、查询“张旭“教师任课的学生成绩

select degree from score where cno in(select cno from coure where tno in(select tno from teacher where tname='张旭'));

2、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

select * from score s1 right join score s2 on s1.sno=s2.sno and s1.degree>s2.degree left join score s3 on s3.sno=s2.sno and s2.degree>s3.degree where s1.sno is null and s3.sno in(select sno from score group by score.sno having count(cno)>1);

方法二:

select s1.*,s2.* from (select sno,max(degree) degree from score where sno in(select sno from score group by score.sno having count(cno)>1) group by score.sno) s1 right join score s2 on s1.sno=s2.sno where s1.degree>s2.degree;

第二高

select *,max(s3.degree) from score s1 right join score s2 on s1.sno=s2.sno and s1.degree>s2.degree left join score s3 on s3.sno=s2.sno and s2.degree>s3.degree where s1.sno is null and s3.sno in(select sno from score group by score.sno having count(cno)>1) group by s3.sno;

方法二:

select s1.*,s2.*,max(s2.degree) from (select sno,max(degree) degree from score where sno in(select sno from score group by score.sno having count(cno)>1) group by score.sno) s1 right join score s2 on s1.sno=s2.sno where s1.degree>s2.degree group by s2.sno;

3、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree>(select degree from score where sno=109 and cno='3-105');

4、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select sno,sname,sbirthday from stu where sbirthday=(select sbirthday from stu where sno=108);

5、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from score join stu on score.sno=stu.sno where degree>(select degree from score where sno=109 and cno='3-105');

6、查询选修某课程的同学人数多于5人的教师姓名。

select * from score join coure on score.cno=coure.cno join teacher on coure.tno=teacher.tno group by score.cno having count(score.sno)>5;

7、查询95033班和95031班全体学生的记录。

select * from stu where class in(95033,95031);

8、查询存在有85分以上成绩的课程

select cname from score join coure on score.cno=coure.cno group by score.cno having max(score.degree)>85;

9、查询出“计算机系“教师所教课程的成绩表。

select * from score  where cno in(select cno from coure where tno in(select tno from teacher where depart='计算机系'));

另:例如查询前几名的sql 最好不用milit 可以临时表分组排序取第n各值(n为最小值)然后大于它全部显示

发布了48 篇原创文章 · 获赞 1 · 访问量 3834

猜你喜欢

转载自blog.csdn.net/qq_43840143/article/details/103081749