ret=Emp.objects.values('dep').annotate(avg_salary=Avg('salary'))
print(ret)
# ---*******单表分组查询ORM总结:表名.objects.values('group by 的字段').annotate(聚合函数('统计的字段')
from django.db.models import Avg,Count,Min,Max ret = models.Publish.objects.values('nid').annocate(c=Count('book__name')) print(ret) # <querySet[{'nid':'ccc','c':'xxx'}]> ret = models.Publish.objects.values('nid').annocate(c=Count('book__name')).values('name','c') print(ret) # <querySet[{'name':'ccc','c':'xxx'}]> ret = models.Publish.objects.values('nid').annocate(c=Count('book__name')).filter(name='人民出版社').values('name','c') print(ret) # <querySet[{'name':'ccc','c':'xxx'}]>,这里面的filter相当于having,先group_by后having
其他可以参照类似上诉格式