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;
    ————取自姚老師筆記



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章