Django-查詢相關練習

一:準備表結構

 1 from django.db import models
 2 
 3 # Create your models here.
 4 
 5 class Student(models.Model):
 6     sid=models.AutoField(primary_key=True)
 7     sname=models.CharField(max_length=32)
 8     gender=models.CharField(max_length=32)
 9     clas=models.ForeignKey(to="Class",on_delete=models.CASCADE)
10     def __str__(self):
11         return self.sname
12 
13 class Class(models.Model):
14     cid=models.AutoField(primary_key=True)
15     caption=models.CharField(max_length=32)
16     grade=models.ForeignKey(to="Class_grade",on_delete=models.CASCADE)
17     teacher=models.ManyToManyField(to='Teacher')
18     def __str__(self):
19         return self.caption
20 
21 class Class_grade(models.Model):
22     gid=models.AutoField(primary_key=True)
23     gname=models.CharField(max_length=32)
24     def __str__(self):
25         return self.gname
26 
27 
28 class Course(models.Model):
29     cid=models.AutoField(primary_key=True)
30     cname=models.CharField(max_length=32)
31     teacher=models.ForeignKey(to='Teacher',on_delete=models.CASCADE)
32     def __str__(self):
33         return self.cname
34 
35 class Teacher(models.Model):
36     tid=models.AutoField(primary_key=True)
37     tname=models.CharField(max_length=32)
38     def __str__(self):
39         return self.tname
40 class Score(models.Model):
41     sid=models.AutoField(primary_key=True)
42     student=models.ForeignKey(to='Student',on_delete=models.CASCADE)
43     course=models.ForeignKey(to='Course',on_delete=models.CASCADE)
44     score=models.IntegerField()
45     def __str__(self):
46         return self.score
models.py

 

二:習題

# # 1 查詢學生總人數;
    # ret1=Student.objects.count()
    # print(ret1)
    # # 2 查詢「生物」課程和「物理」課程成績都及格的學生id和姓名;
    # ret2=Score.objects.filter(course__cname__in=["生物","物理"],score__gte=60).values("student__sname","student__pk")
    #
    # # 3 查詢每一個年級的班級數,取出班級數最多的前三個年級名稱;
    # ret3=Grade.objects.annotate(c=Count("klass")).order_by("-c")[:3]
    # print(ret3)
    #
    # # 4 查詢平均成績最高的學生的id和姓名以及平均成績;
    # ret=Score.objects.values("student").annotate(avg_score=Avg("score")).order_by("-avg_score").values("student__sname","student__pk","avg_score")[0]
    # print(ret)
    #
    # # 5 查詢每一個年級的學生人數;
    # ret=Grade.objects.annotate(c=Count("klass__student")).values("gname","c")
    # print(ret)
    #
    # # 6 查詢每位學生的學號,姓名,平均成績;
    #
    # ret=Student.objects.annotate(avg_score=Avg("score__score")).values("sname","pk","avg_score")
    # print(ret)

    # 7 查詢學生編號爲「2」的學生的姓名、該學生成績最高的課程名及分數;
    # ret=Score.objects.values("student").annotate(max_score=Max("score")).values("student__sname","max_score","course__cname")
    # ret = Score.objects.filter(student=2).order_by("-score").values("score", "course__cname", "student__sname")[0]
    # print(ret)

    # # 8 查詢每個姓「李」的老師所帶班級數;
    # ret=Teacher.objects.filter(tname__startswith="李").annotate(c=Count("classes")).values("tname","c")

    # # 9 查詢班級數小於5的年級id和年級名;
    # ret=Grade.objects.annotate(c=Count("klass")).filter(c__lt=5).values("pk","gname")

    # # 10 查詢教過課程超過2門的老師的id和姓名;
    # ret=Teacher.objects.annotate(c=Count("course")).filter(c__gt=2).values("pk","tname")
    #
    # # 11 查詢學過編號「1」課程和編號「2」課程的同窗的學號、姓名;
    # ret=Student.objects.filter(score__course__cid__in=[1,2]).values("pk","sname")
    #
    # # 12 查詢所帶班級數最多的老師id和姓名;
    # ret=Teacher.objects.annotate(c=Count("classes")).order_by("-c").values("pk","tname")[0]
    #
    # # 13 查詢有課程成績小於60分的同窗的學號、姓名;
    # ret=Score.objects.filter(score__lt=60).values("student__sname","student__pk").distinct()
    #
    # # 14 查詢男生、女生的人數,按倒序排列;
    # ret=Student.objects.values("gender").annotate(c=Count(1)).order_by("c").values("gender","c")
    #
    # # 15 查詢各個課程及相應的選修人數;
    # ret=Score.objects.values("course").annotate(c=Count("student")).values("course__cname","c")
    #
    # # 16 查詢同時選修了物理課和生物課的學生id和姓名;
    # ret=Student.objects.filter(score__course__cname__in=["物理","生物"]).values("pk","sname")
    #
    # # 17 檢索「3」課程分數小於60,按分數降序排列的同窗學號;
    # ret=Score.objects.filter(course__cid=3,score__lt=60).order_by("score").values("student__sname","student__pk")
    #
    # # 18 查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
    # ret=Score.objects.values("course").annotate(avg_score=Avg("score")).order_by("avg_score","course")
    #
    # # 19 查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;
    # ret=Course.objects.annotate(max_score=Max("score__score"),min_score=Min("score__score")).values("cname","max_score","min_score")
View Code
相關文章
相關標籤/搜索