zoukankan      html  css  js  c++  java
  • django 聚合统计查询

    from django.shortcuts import render
    from django.http import HttpResponse
    from django.db.models import Sum,Max,Min,Avg
    from grade.models import Grade, Student


    """ 聚合及统计 """
    # 计算张三的期末成绩总和
    # grade_list = Grade.objects.filter(student_name='张三').aggregate(Sum('score'))
    # grade_list = Grade.objects.filter(student_name='张三').aggregate(total = Sum('score'))

    # 求语文最高分
    # grade_list = Grade.objects.filter(subject_name='语文').aggregate(high_score=Max('score'))
    # print(grade_list['high_score'])
    # # 最低分
    # grade_list = Grade.objects.filter(subject_name='语文').aggregate(min_score=Min('score'))
    # print(grade_list['min_score'])

    # 平均分
    # grade_list = Grade.objects.filter(subject_name='语文').aggregate(avg_score=Avg('score'))
    # print(grade_list['avg_score'])

    # 统计每个学生的成绩总和
    # total_list = Grade.objects.values_list('student_name').annotate(Sum('score'))
    # for item in total_list:
    # print(item)

    # user_zhang = Student.objects.get(pk=1)
    # list = user_zhang.stu_grade.all()
    # for item in list:
    # print(item.subject_name,item.score)

    user_list = Student.objects.all().annotate(total_score = Sum('stu_grade__score'))
    for user in user_list:
    print(user.student_name,user.total_score)
  • 相关阅读:
    mac前端解除端口占用命令
    Mac安装Brew
    Mac Webstrom 快捷键
    linux yarn安装
    js-新兴的API,最佳实践,离线应用于客户端存储
    js-高级技术
    js-Ajax与Comet
    js-错误处理与调试,JSON
    js-DOM2,表单脚本
    js-事件
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11474689.html
Copyright © 2011-2022 走看看