django 中group by 以及sum count

发布时间:2024年01月19日

原生SQL

SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(evening_hours),sum(total_hours) 
FROM orders 
GROUPBY order_id,city,locality,login_time`

group by … sum

from django.db.models import Sum

Your_Model.objects.values(
    "order_id", "city", "locality", "login_time"
).order_by().annotate(
    Sum("morning_hours"),
    Sum("afternoon_hours"),
    Sum("evening_hours"),
    Sum("total_hours"),
)

group by …count

 from django.db.models import Count

 result = Books.objects.values('author')
                       .order_by('author')
                       .annotate(count=Count('author'))

https://docs.djangoproject.com/en/4.2/topics/db/aggregation/

文章来源:https://blog.csdn.net/weixin_43632687/article/details/130088199
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。