models(创建表):
class Grade(models.Model): """年级表""" gname=models.CharField(max_length=32) class Classes(models.Model): """班级表""" caption=models.CharField(max_length=32) grade=models.ForeignKey("Grade", on_delete=models.CASCADE) teach_cls = models.ManyToManyField('Teacher') class Teacher(models.Model): """老师表""" tname=models.CharField(max_length=32) class Course(models.Model): """课程表""" cname=models.CharField(max_length=32) teacher=models.OneToOneField("Teacher", on_delete=models.CASCADE) class Student(models.Model): """学生表""" sname = models.CharField(max_length=32) gender = models.CharField(max_length=10) classes = models.ForeignKey("Classes",on_delete=models.CASCADE) class Score(models.Model): """成绩表""" student = models.ForeignKey("Student",on_delete=models.CASCADE) course = models.ForeignKey("Course",on_delete=models.CASCADE) score=models.IntegerField()
插入数据:
def orm_insert(request): """orm表记录插入""" # 教师表插入 t1 = Teacher.objects.create(tname="张三") t2 = Teacher.objects.create(tname="李四") t3 = Teacher.objects.create(tname="王五") # 年级表插入 g1 = Grade.objects.create(gname="一年级") g2 = Grade.objects.create(gname="二年级") g3 = Grade.objects.create(gname="三年级") # 课程表插入 c1 = Course.objects.create(cname="生物",teacher_id=1) c2 = Course.objects.create(cname="体育",teacher_id=2) c3 = Course.objects.create(cname="物理",teacher_id=3) # 班级表 cl1 = Classes.objects.create(caption="一年一班",grade_id=1) cl11 = Classes.objects.create(caption="一年二班",grade_id=1) cl2 = Classes.objects.create(caption="二年一班",grade_id=2) cl22 = Classes.objects.create(caption="二年二班",grade_id=2) cl3 = Classes.objects.create(caption="三年一班",grade_id=3) cl33 = Classes.objects.create(caption="三年二班",grade_id=3) cl1.teach_cls.add(t1,t2,t3) cl11.teach_cls.add(t1) cl2.teach_cls.add(t2,t3) cl22.teach_cls.add(t1,t3) cl3.teach_cls.add(t3) cl33.teach_cls.add(t1,t2,t3) # 学生表 s1 = Student.objects.create(sname="刘龙康",gender='男',classes_id=1) s2 = Student.objects.create(sname="小胖",gender='男',classes_id=1) s3 = Student.objects.create(sname="小明",gender='男',classes_id=2) s4 = Student.objects.create(sname="小红",gender='女',classes_id=3) s5 = Student.objects.create(sname="小花",gender='女',classes_id=4) s6 = Student.objects.create(sname="刘能",gender='女',classes_id=5) s7 = Student.objects.create(sname="胖妞",gender='女',classes_id=1) # 成绩表 sc1 = Score.objects.create(student=s1,course=c1,score=60) sc2 = Score.objects.create(student=s1,course=c2,score=80) sc3 = Score.objects.create(student=s1,course=c3,score=90) sc4 = Score.objects.create(student=s2,course=c1,score=60) sc5 = Score.objects.create(student=s2,course=c2,score=70) sc6 = Score.objects.create(student=s7,course=c2,score=100) sc7 = Score.objects.create(student=s3,course=c3,score=30) sc8 = Score.objects.create(student=s4,course=c1,score=20) sc9 = Score.objects.create(student=s5,course=c1,score=60) sc10 = Score.objects.create(student=s6,course=c1,score=80) sc11 = Score.objects.create(student=s7,course=c1,score=20) return HttpResponse("ok")
查询练习:
def orm_search(request): """orm作业查询""" # 1.查询学生总人数; # ret = Student.objects.aggregate(student_count=Count('id')) # ret = Student.objects.count() # 2.查询生物课程和物理课程成绩都及格的学生id和姓名 # ret = Student.objects.annotate(jgs=Count('id')).filter(score__course__cname__in=['生物','物理'],score__score__gte=60,jgs=2).values("id","sname") # 3.查询每个年级的班级数,取出班级数最多的前二个年级 # ret = Grade.objects.annotate(cls_count=Count('classes__grade')).order_by('-cls_count').values('gname','cls_count') # 4.查询平均成绩最高的学生的id和姓名以及平均成绩 # ret = Student.objects.annotate(avg_score=Avg('score__score')).order_by('-avg_score').values('id','sname','avg_score')[0] # 5.查询每个年级的学生人数 # ret = Grade.objects.annotate(stu_count=Count('classes__student__id')).values('gname','stu_count') # 6.查询每位学生的学号,姓名,平均成绩 # ret = Student.objects.annotate(stu_avg=Avg('score__score')).values('id','sname','stu_avg') # 7.查询学生编号为2的学生姓名,该学生成绩最高的课程名及分数 # ret = Student.objects.filter(id=2).values('sname','score__course__cname','score__score').order_by('-score__score')[0] # ret = Student.objects.filter(id="2").annotate(scoreMax=Max("score__score")).order_by('-scoreMax')[0:1].values("sname", "score__course__cname", "scoreMax") # 8.查询姓李的老师的个数和所带班级数 # ret = Teacher.objects.filter(tname__startswith='李').annotate(cls_count=Count('classes')).values('tname','cls_count') # 9.查询班级数小于3的年级id和年级名 # ret = Grade.objects.annotate(cls_count=Count('classes')).values('id','gname','cls_count').filter(cls_count__lt=3) # 10.查询教过课程超过2门的老师的id和姓名 # ret = Teacher.objects.annotate(cour_count=Count('course')).filter(cour_count__gte=1).values('id','tname','cour_count') # 11.查询教过超过3个班级的老师的id和姓名 # ret = Teacher.objects.annotate(cls_count=Count('classes')).filter(cls_count__gte=3).values('id','tname','cls_count') # 12.查询学过编号1课程和编号2课程的同学的学号,姓名 # ret = Student.objects.filter(score__course__in=[1,2]).annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count') # 13.查询只学过编号1课程和编号2课程的同学的学号,姓名 # ret1 = Student.objects.annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count') # ret2 = Student.objects.filter(score__course__in=[1,2]).annotate(course_count=Count('score')).filter(course_count=2).values('id','sname','course_count') # for i in ret1: # for j in ret2: # if i==j: # print(i) # 这个方法好 # ret = Student.objects.annotate(course_count=Count('score')).filter(score__course=1).filter(score__course=2).filter(course_count=2).values('id','sname') # 14.查询所带班级数最多的老师id和姓名 # ret = Teacher.objects.annotate(cls_count=Count('classes')).values('id','tname','cls_count').order_by('-cls_count')[0] # 15.查询有课程成绩小于60分的同学的学号,姓名 # ret = Student.objects.filter(score__score__lt=60).annotate(stu_score=Count('score')).filter(stu_score__gte=1).values('id','sname','stu_score') # 16.查询男生,女生的人数,按倒序排列 # ret = Student.objects.values('gender').annotate(c=Count('id')).order_by('-c') # 17.查询各个课程及相应的选修人数 # ret = Course.objects.annotate(stu_count=Count('score')).values('cname','stu_count') # 18.查询同时选修了物理课和生物课的学生id和姓名 # ret = Student.objects.filter(score__course__cname__in=['物理','生物']).annotate(course_count=Count('score__course')).filter(course_count=2).values('id','sname','course_count') # ret = Student.objects.annotate(c=Count('score__course')).filter(c=2).filter(score__course__cname="物理").filter(score__course__cname="生物").values("id", "sname") # 19.检索3课程分数小于60,按分数降序排列的同学学号 # ret = Course.objects.filter(score__course=3,score__score__lt=60).order_by('score__score').values('score__student','score__student__sname') # 20.检查每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 # ret = Course.objects.annotate(score_avg=Avg('score__score')).order_by('score_avg','-id').values('id','cname','score_avg') # 21.查询各科成绩最高和最低分:以如下形式显示:课程ID,最高分,最低分 ret = Course.objects.annotate(max_score=Max('score__score'),min_score=Min('score__score')).values('id','cname','max_score','min_score') print(ret) return HttpResponse("ok")