使用之前学到过的操作实现下面的查询操作:
1. 查询平均成绩大于60分的同学的id和平均成绩;
2. 查询所有同学的id、姓名、选课的数量、总成绩;
3. 查询姓“李”的老师的个数;
4. 查询没学过“李老师”课的同学的id、姓名;
5. 查询学过课程id为1和2的所有同学的id、姓名;
6. 查询学过“黄老师”所教的“所有课”的同学的id、姓名; 7. 查询所有课程成绩小于60分的同学的id和姓名;
8. 查询没有学全所有课的同学的id、姓名;
9. 查询所有学生的姓名、平均分,并且按照平均分从高到低排序;
10. 查询各科成绩的最高和最低分,以如下形式显示:课程ID,课程名称,最高分,最低分;
11. 查询没门课程的平均成绩,按照平均成绩进行排序; 12. 统计总共有多少女生,多少男生;
13. 将“黄老师”的每一门课程都在原来的基础之上加5分;
14. 查询两门以上不及格的同学的id、姓名、以及不及格课程数;
15. 查询每门课的选课人数;
from django.shortcuts import render from django.http import HttpResponse from .models import Student,Teacher,Score,Course from django.db.models import Avg,Sum,Count,Q,F,Min,Max def index(request): #1. 查询平均成绩大于60分的同学的id和平均成绩; students = Student.objects.annotate(score_avg = Avg("score__number")).filter(score_avg__gt=60).values('id','score_avg') for student in students: print(student) return HttpResponse('123') def index2(request): #2. 查询所有同学的id、姓名、选课的数量、总成绩; students = Student.objects.annotate(course_num = Count('score'),total = Sum('score__number')).values('id','course_num','total','name') for student in students: print(student) return HttpResponse('456') def index3(request): #3.查询姓“李”的老师的个数; count = Teacher.objects.filter(name__startswith='李').count() print(count) return HttpResponse('789') def index4(request): #4.查询没学过“李老师”课的同学的id、姓名; students = Student.objects.exclude(score__course__teacher__name = '李老师').values('id','name') for student in students: print(student) return HttpResponse('741') def index5(request): # 5.查询学过课程id为1和2的所有同学的id、姓名 students = Student.objects.filter(score__course__in=[1.2]).values('id','name') for student in students: print(student) return HttpResponse('852') def index6(request): # 6.查询学过“黄老师”所教的所有课的同学的学号、姓名; # 1. 首先先找到每一位学生学习的黄老师课程的数量;A # 2. 在课程的表中找到黄老师教的课程的数量;B # 3. 判断A是否等于B,如果相等,那么意味着这位学生学习了黄老师教的 # 所有课程,如果不想等,那么意味着这位学生没有学完黄老师教的所有课程 rows = Student.objects.annotate(nums =Count("score__course",filter=Q(score__course__teacher__name='黄老师'))).filter(nums =Course.objects.filter(teacher__name='黄老师').count()).values('id','name') for row in rows: print(row) return HttpResponse('00000') def index7(request): # 7. 查询所有课程成绩小于60分的同学的id和姓名; students = Student.objects.filter(score__number__lt=60).values('id','name') print(students) for student in students: print(student) return HttpResponse('159') def index8(request): # 8. 查询没有学全所有课的同学的id、姓名; # students = Student.objects.annotate(num=Count(F('score__course'))) students = Student.objects.annotate(num = Count(F('score__course'))).filter(num__lt=Course.objects.count()).values('id','name') print(students) return HttpResponse('index8') def index9(request): # 9.查询所有学生的姓名、平均分,并且按照平均分从高到低排序; students =Student.objects.annotate(avg = Avg('score__course')).order_by('-avg').values('name','avg') for student in students: print(student) return HttpResponse('index9') def index10(request): # 10.查询各科成绩的最高和最低分,以如下形式显示:课程ID,课程名称,最高分,最低分: courses = Course.objects.annotate(min = Min("score__number"),max=Max("score__number")).values('id','name','min','max') for course in courses: print(course) return HttpResponse("index10") def index11(request): # 11. 查询每门课程的平均成绩,按照平均成绩进行排序; courses = Course.objects.annotate(avg =Avg("score__number")).order_by("avg").values("id",'name','avg') for course in courses: print(course) return HttpResponse('index11') def index12(request): # 12. 统计总共有多少女生,多少男生 rows = Student.objects.aggregate(male_num=Count('gender',filter=Q(gender=1)),female_num =Count('gender',filter=Q(gender=2))) print(rows) return HttpResponse('index12') def index13(request): # 13.将“黄老师”的每一门课程都在原来的基础之上加5分; rows = Score.objects.filter(course__teacher__name='黄老师').update(number=F('number')+5) print(rows) return HttpResponse('index13') def index14(request): #14 .查询两门以上不及格的同学的id、姓名、以及不及格课程数 students = Student.objects.annotate(bad_num = Count('score__number',filter=Q(score__number__lt=60))).filter(bad_num__gte=2).values('id','name','bad_num') for student in students: print(student) return HttpResponse('index14') def index15(request): #15. 查询每门课的选课人数; courses = Course.objects.annotate(student_num = Count('score__student')).values('name','student_num') for course in courses: print(course) return HttpResponse('index15')