django 一个关于分组查询的问题分析

 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

其他可以参照类似上诉格式

上一篇:What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?


下一篇:Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作