Django分组查询

先补充两个知识点:

1.group by

大前提:可以按照任意字段分组,但是最好是按照分辨度比较低的来分组(重复比较多的分辨度比较低)。

group by分组可以单独使用,不搭配其他条件。

分组的字段重复的会被隐去,如果想查看可以用固定的函数去查看。

分组主要是为了查看分组后该字段每个组的个数

select post,count(*) from emp where id>3 group by post having count(id) >5

2.having

having是将通过where查询出的记录再次进行过滤,可以使用聚合函数。

下面可以进入Django分组查询正题:

annotate()为调用的QuerySet中每一个对象都生成一个独立的统计值(统计方法用聚合函数)。

总结 :跨表分组查询本质就是将关联表join成一张表,再按单表的思路进行分组查询。

把分组出来的每一个对象都进行聚合!!!

 1  # 分组
 2     # 统计每一本书作者个数
 3     from django.db.models import Count,Min,Max
 4     # ret=Book.objects.all().annotate(author_num=Count('authors__nid')).values('name','author_num')
 5     # values 在这里代指group by 的字段
 6     # ret=Book.objects.all().values('name').annotate(author_num=Count('authors__name')).values('name','author_num')
 7     # print(ret)
 8     # for book in ret:
 9     #     print(book.name,":",book.c)
10     # print(ret)
11     # 统计每一个出版社的最便宜的书
12     # ret=Publish.objects.all().annotate(c=Min('book__price')).values('name','c')
13     # for pub in ret:
14     #     print(pub.name,":",pub.c)
15     # print(ret)
16     # 统计每一本以py开头的书籍的作者个数:
17     # ret=Book.objects.all().filter(name__startswith='红').annotate(c=Count('authors__nid')).values('name','c')
18     # print(ret)
19     #统计不止一个作者的图书:(作者数量大于一)
20     # 注意:valu再annotate前,代表group by 的字段,不写value,默认以基表的主键做group by 在后代表我要select出来的字段
21     # filter在前,代指where的东西,在后,代表having 的东西
22     # ret = Book.objects.all().values('name').annotate(author_num=Count('authors__name')).filter(author_num__gt=1).values('name','author_num')
23     # print(ret)
24     # 练习:查询各个作者出的书的总价格:
25     # ret=Author.objects.all().annotate(c=Sum('book__price')).values('name','c')
26     # print(ret)
27     # 查询每个出版社的名称和书籍个数
28     # ret=Publish.objects.all().annotate(c=Count('book__nid')).values('name','c')
29     # ret=Publish.objects.all().annotate(c=Count('book')).values('name','c')
30     # print(ret)

 

原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9635284.html