javaSQL语句 四表查询实例

student(id,name,age)学生表(学生编号,学生姓名,学生年龄)
course(id,name,teacher_id)课程表(课程编号,课程名称,老师编号)
grade(id,studnt_id,course_id,grade)成绩表(成绩编号,学生编号,课程编号,成绩)
teacher(id,name,age,salary )(老师编号,老师姓名,老师年龄,老师工资)
1.找出没有选修李明老师课程的所有学生姓名
2.查看学生年龄低于20的考试成绩和科目
3.查看小明的考试成绩和考试科目及了老师姓名
4.查看成绩低于60分的学生是哪个老师教的
5.查看各个老师所授课程的平均分
6.老师平均分低于60的老师工资减少1000;
7.删除考试成绩大于100的学生信息
8.将没有成绩的学生从成绩表中删除(将成绩null的记录删除)
搭架子:select * from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id;
1.select stu.name from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id and t.name!='李明';
2.select g.grade,c.name from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id and stu.age<20;
3.select g.grade,c.name,t.name from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id and stu.name='小明';
4.select t.name from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id and g.grade<60;
    
5.select t.id,avg(g.grade) from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id group by t.id ;
6.update teacher set salary=salary-1000 where id in(
    select t.id from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id group by t.id having avg(g.grade)<60;
)
7.delete from student where id in(
    select stu.id from student stu,teacher t,grade g,course c where
        stu.id=g.studnt_id and g.course_id=c.id and c.teacher_id=t.id and g.grade>100)
简单写法:
delete from student where id in(
    select stu.id from grade g,student stu where g.studnt_id=stu.id and g.grade>100)
8.delete from grade where grade is null;
    ————取自姚老师笔记



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值