作者:云下拾雨 | 来源:互联网 | 2023-10-11 14:17
聚合函数引包:fromdjango.db.modelsimportAvg,Sum,Min,Max,Count查询count1Article.objects.all().aggreg
聚合函数引包:
from django.db.models import Avg,Sum,Min,Max,Count
查询
count1 = Article.objects.all().aggregate(Count("title"))
加筛选条件查询:
count2 = Article.objects.filter(cOntext=‘xt‘).aggregate(Count("title"))
分组之后查询:
count3 = Article.objects.values("context").annotate(Count("title"))
多表查询:
count3=Account.objects.filter(password="123456").aggregate(Count(‘article__context‘))
account 是外键的表,对应sql
SELECT COUNT(`myapp_article`.`context`) AS `article__context__count` FROM `myapp_account` LEFT OUTER JOIN `myapp_article` ON (`myapp_accoun
t`.`id` = `myapp_article`.`account_id`) WHERE `myapp_account`.`password` = ‘123456‘; args=(‘123456‘,)