import os
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day77.settings")
import django
django.setup()
from app01.models import *
# 基于双 下划线的一对多查询
# 查询出版社为北京出版社出版的所有图书的名字,价格
# ret=Publish.objects.filter(name='北京出版社').values('book__name','book__price')
# print(ret)
# ret=Book.objects.filter(publish__name='北京出版社').values('name','price')
# print(ret)
# 查询北京出版社出版的价格大于19的书
# ret=Publish.objects.filter(name='北京出版社',book__price__gt=19).values('book__name','book__price')
# print(ret)
# 多对多
# 查询红楼梦的所有作者名字
# ret=Book.objects.filter(name='红楼梦').values('authors__name')
# print(ret)
# ret=Author.objects.filter(book__name='红楼梦').values('name')
# print(ret)
# 查询图书价格大于30的所有作者名字
# ret=Book.objects.filter(price__gt=30).values('authors__name')
# print(ret)
# 进阶练习--连续跨表
# 查询北京出版社出版过的所有书籍的名字以及作者的姓名
# ret=Publish.objects.filter(name='北京出版社').values('book__name','book__authors__name')
# print(ret)
# ret=Book.objects.filter(publish__name='北京出版社').values('name','authors__name')
# print(ret)
# 手机号以151开头的作者出版过的所有书籍名称以及出版社名称
# ret=AuthorDetail.objects.filter(phone__startswith='13').values('author__book__name','author__book__publish__name')
# print(ret)
# ret=Book.objects.filter(authors__authordetail__phone__startswith='13').values('name','publish__name')
#
# print(ret)
# 聚合查询aggregate
from django.db.models import Avg,Count,Max,Min,Sum
# 计算所有图书的平均价格
# ret=Book.objects.all().aggregate(Avg('price'))
# print(ret)
# 计算图书的最高价格
# ret=Book.objects.all().aggregate(Max('price'))
# print(ret)
#他是queryset的终止子句
# 计算图书的最高价格,最低价格,平均价格,总价
#
# ret=Book.objects.all().aggregate(Max('price'),Min('price'),Avg('price'),Sum('price'))
# print(ret)
#分组查询annotate()
# 统计每一本书作者个数
# ret=Book.objects.all().annotate(c=Count('authors'))
# print(ret)
# for r in ret:
# print(r.name,'---->',r.c)
# ret=Book.objects.all().annotate(c=Count('authors')).values('name','c')
# print(ret)
# 统计每一个出版社的最便宜的书(以谁group by 就以谁为基表)
# ret=Publish.objects.all().annotate(m=Min('book__price')).values('name','m')
# print(ret)
# 统计每一本以py开头的书籍的作者个数
# ret1=Book.objects.all().filter(name__startswith='py').annotate(c=Count('authors')).values('name','c')
# print(ret1)
# 总结: group by 谁,就以谁做基表,filter过滤,annotate取分组,values取值
# 总结终极版本
# values在前,表示group by 在后,表示取值
# filter在前,表示where条件,在后表示having
# 统计每一本以py开头的书籍的作者个数--套用模板
# ret2=Book.objects.all().values('name').filter(name__startswith='py').annotate(c=Count('authors')).values('name','c')
# print(ret2)
# 查询各个作者出的书的总价格
# ret=Author.objects.all().values('name').annotate(s=Sum('book__price')).values('name','s')
# ret=Author.objects.all().annotate(s=Sum('book__price')).values('name','s')
# print(ret)
#查询名字叫lqz作者书的总价格
# ret=Author.objects.all().values('pk').filter(name='lqz').annotate(s=Sum('book__price')).values('name','s')
# print(ret)
# 查询所有作者写的书的总价格大于30
# ret=Author.objects.all().values('pk').annotate(s=Sum('book__price')).filter(s__gt=2).values('name','s')
# ret=Author.objects.all().annotate(s=Sum('book__price')).filter(s__gt=30).values('name','s')
# print(ret)
# 总结终极版本
# values在前,表示group by 在后,表示取值
# filter在前,表示where条件,在后表示having
# 统计不止一个作者的图书
ret=Book.objects.all().values('pk').annotate(c=Count('authors')).filter(c__gt=1).values('name','c')
# ret = Book.objects.annotate(author_num=Count("authors")).filter(author_num__gt=1).values('name','author_num')
print(ret)