python-資料庫查詢操作(聚合函式)-aggregate及annotate用法
阿新 • • 發佈:2021-01-13
book
book_order
modles.py
from django.db import models
from django.db import models
class Author(models.Model):
"""作者模型"""
name = models.CharField(max_length=100)
age = models.IntegerField()
email = models.EmailField()
class Meta:
db_table = 'author'
class Publisher(models.Model):
"""出版社模型"""
name = models.CharField(max_length=300)
class Meta:
db_table = 'publisher'
class Book(models.Model):
"""圖書模型"""
name = models.CharField(max_length=300)
pages = models.IntegerField()
price = models.FloatField()
rating = models.FloatField()
author = models.ForeignKey(Author,on_delete=models.CASCADE)
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
class Meta:
db_table = 'book'
class BookOrder(models.Model):
"""圖書訂單模型"""
book = models.ForeignKey("Book", on_delete=models.CASCADE)
price = models.FloatField()
create_time = models.DateTimeField(auto_now_add=True, null=True)
class Meta:
db_table = 'book_order'
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book, Author,BookOrder
from django.db.models import Avg, Count, Max, Min, Sum
from django.db import connection
def avg_func(request):
# 獲取所有圖書的價格平均值 avg
# filter 過濾條件
# 聚合函式 aggregate
# book = Book.objects.aggregate(avg_price=Avg('price'))
# id大於2價格平均值
book = Book.objects.filter(id__gt=2).aggregate(avg_price=Avg('price'))
print(book)
# print(type(book))
print(connection.queries)
return HttpResponse("avg")
def agg_ann_func(request):
# 每一本圖書的銷售的平均價格
# annotate 分組反回值型別->querySet aggregate 不分組 用於聚合函式
# books = Book.objects.aggregate(avg=Avg('bookorder__price'))
books = Book.objects.annotate(avg=Avg('bookorder__price')).filter(avg__gt=90)
for book in books:
print(book.name, book.avg)
print(connection.queries)
return HttpResponse("agg_ann_func")
def count_func(request):
# 查詢一共有多少本圖書
# books = Book.objects.aggregate(Count("id"))
# books = Book.objects.aggregate(Count("pk"))
# books = Book.objects.aggregate(book_nums=Count("pk"))
# print(books)
# 查詢作者 年齡不重複的有多少個
# authors = Author.objects.aggregate(count=Count("age", distinct=True))
# print(authors)
# 每本圖書的銷量 __id可以省略不寫
# books = Book.objects.annotate(book_num=Count("bookorder__id"))
books = Book.objects.annotate(book_num=Count("bookorder"))
for book in books:
print(book.name, book.book_num)
print(connection.queries[-1])
return HttpResponse("count_func")
def max_min_func(request):
# author = Author.objects.aggregate(Max("age"), Min("age"))
# print(author)
# 每本圖書的售賣值的最大和最小
books = Book.objects.annotate(max=Max("bookorder__price"), min=Min("bookorder__price"))
for book in books:
print(book.name, book.max, book.min)
return HttpResponse("max_min_func")
def sum_func(request):
# 圖書銷售的總額
# book = BookOrder.objects.aggregate(total=Sum("price"))
# print(book)
# 每一本圖書銷售的總額
# books = Book.objects.annotate(total=Sum("bookorder__price"))
# for book in books:
# print(book.name, book.total)
#
# 統計2020年 銷售總額
books = BookOrder.objects.filter(create_time__year=2020).aggregate(total=Sum("price"))
print(books)
# 統計每本圖書2020年 銷售總額
books = Book.objects.filter(bookorder__create_time__year=2020).annotate(total=Sum("bookorder__price"))
for book in books:
print(book.name, book.total)
return HttpResponse("sum_func")