博客信息

Python Django 模型层(聚合函数)

发布时间:『 2019-08-15 02:18』  博客类别:Python  阅读(640)

构建一个新的app,名叫together应用,然后准备一点数据


小李飞刀_Python


求最高成绩


from together.models import *
def showsql():
    from django.db import connection
    print connection.queries[-1]['sql']

from together.models import *
def showsql():
    from django.db import connection
    print connection.queries[-1]['sql']
    
from django.db.models import Max
Student.objects.aggregate(Max('score'))
{'score__max': 99}
showsql()
SELECT MAX("together_student"."score") AS "score__max" FROM "together_student"


求最低成绩


from django.db.models import Min
Student.objects.aggregate(Min('score'))
{'score__min': 66}
showsql()
SELECT MIN("together_student"."score") AS "score__min" FROM "together_student"


学生总人数


from django.db.models import Count
Student.objects.aggregate(Count('sno'))
{'sno__count': 6}
showsql()
SELECT COUNT("together_student"."sno") AS "sno__count" FROM "together_student"


求总成绩


from django.db.models import Sum
Student.objects.aggregate(Sum('score'))
{'score__sum': 497}
showsql()
SELECT SUM("together_student"."score") AS "score__sum" FROM "together_student"


求平均成绩


from django.db.models import Avg
Student.objects.aggregate(Avg('score'))
{'score__avg': 82.83333333333333}
showsql()
SELECT AVG("together_student"."score") AS "score__avg" FROM "together_student"


分组聚合函数

由下面可以看出,默认是按表中的所有列段进行分组的

Student.objects.annotate(c=Count('sno'))
<QuerySet [<Student: Student:泰迪>, <Student: Student:汪汪>, <Student: Student:旺旺大礼包>, <Student: Student:隔壁老王>, <Student: Student:小迷妹>, <Student: Student:皮卡丘>]>
showsql()
SELECT "together_student"."sno", "together_student"."sname", "together_student"."score", "together_student"."clazz_id", COUNT("together_student"."sno") AS "c" FROM "together_student" GROUP BY "together_student"."sno", "together_student"."sname", "together_student"."score", "together_student"."clazz_id" LIMIT 21
Student.objects.annotate(c=Avg('sno'))
<QuerySet [<Student: Student:泰迪>, <Student: Student:汪汪>, <Student: Student:旺旺大礼包>, <Student: Student:隔壁老王>, <Student: Student:小迷妹>, <Student: Student:皮卡丘>]>
showsql()
SELECT "together_student"."sno", "together_student"."sname", "together_student"."score", "together_student"."clazz_id", AVG("together_student"."sno") AS "c" FROM "together_student" GROUP BY "together_student"."sno", "together_student"."sname", "together_student"."score", "together_student"."clazz_id" LIMIT 21


求每个班学生的总成绩


Student.objects.values('clazz').annotate(s=Sum('score'))
<QuerySet [{'s': 330, 'clazz': 2}, {'s': 167, 'clazz': 3}]>
showsql()
SELECT "together_student"."clazz_id", SUM("together_student"."score") AS "s" FROM "together_student" GROUP BY "together_student"."clazz_id" LIMIT 21


求每个班级的学生人数


SELECT "together_student"."clazz_id", SUM("together_student"."score") AS "s" FROM "together_student" GROUP BY "together_student"."clazz_id" LIMIT 21
Student.objects.values('clazz').annotate(s=Count('score'))
<QuerySet [{'s': 4, 'clazz': 2}, {'s': 2, 'clazz': 3}]>
showsql()
SELECT "together_student"."clazz_id", COUNT("together_student"."score") AS "s" FROM "together_student" GROUP BY "together_student"."clazz_id" LIMIT 21


求每个班级的学生平均成绩


Student.objects.values('clazz').annotate(s=Avg('score'))
<QuerySet [{'s': 82.5, 'clazz': 2}, {'s': 83.5, 'clazz': 3}]>
showsql()
SELECT "together_student"."clazz_id", AVG("together_student"."score") AS "s" FROM "together_student" GROUP BY "together_student"."clazz_id" LIMIT 21


子查询


Student.objects.values('clazz').annotate(s=Sum('score')).aggregate(m=Max('s'))
{'m': 330}
showsql()
SELECT MAX("s") FROM (SELECT "together_student"."clazz_id" AS Col1, SUM("together_student"."score") AS "s" FROM "together_student" GROUP BY "together_student"."clazz_id") subquery


关联查询(这里关联属性使用双下划线)


Student.objects.values('clazz__cname')
<QuerySet [{'clazz__cname': u'python'}, {'clazz__cname': u'python'}, {'clazz__cname': u'python'}, {'clazz__cname': u'python'}, {'clazz__cname': u'.net'}, {'clazz__cname': u'.net'}]>
showsql()
SELECT "together_clazz"."cname" FROM "together_student" INNER JOIN "together_clazz" ON ("together_student"."clazz_id" = "together_clazz"."id") LIMIT 21


over......


关键字:     Python       Django       聚合函数  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有