F polymerization inquiry packet and the query Q

.Orm a supplementary inquiry

1-1 aggregate query

Max Min Sum Avg Count

In mysqlaggregate function it must be used after the packet is not a packet is actually a set of default overall

In the djangomiddle if you want to use aggregate functions must use the keywordaggregate

Also you need to import the module

from django.db.models import Max, Min, Avg, Sum, Count
# 温馨提示 只要跟数据库有关的功能 基本都在 db.models里面 或者 db里面

How to use:

1.赛选出价格最高的书籍
book_obj = models.Book.object.aggregate(mr=Max('price'))
print(book_obj) # 拿到是一个字典 key为mr mr就是价格
2.求书籍总价
book_obj = models.Book.object.aggregate(sm=Sum('price'))
print(book_obj) # {'sm': Decimal('15999.99')}
3.全部一起使用 # 不起名字
book_obj = models.Book.object.aggregate(Max('price'),Sum('price'),..... )
# 返回字典的key 是 查询的字段+__聚合函数

Grouping queries 1-2

mysql usedgroup_by

So when a packet needs such as:? The average salary statistics for each department statistics male to female ratio of each department

django which uses packet: 1. Keyword annotate

2 by means of the aggregate functions

from django.db.models import Max, Min, Avg, Sum, Count
# 统计每一本书的作者个数
res = models.Book.objects.annotate(author_nmu=Count('authors__id'))
print(res) # 返回queryset对象 是个列表
# 统计每一本书的作者个数 书名 和 数量
res = models.Book.objects.annotate(author_num=Count('authors__id')).values('title',author_num)
# author_num 指代的就是作者的个数

# 统计出每个出版社卖的最便宜的价格 出版社名称 图书书名
res = models.Publish.objects.annotate(mi=Min('book__price')).values('name', 'book__titile')

After strict mode packet can only get if want to take based on the packet must be obtained by an aggregate function

Strict Mode can be canceled

# 统计不止一个作者的图书
1. 先拿到书及对应的作者数 
2. 赛选出大于一的图书
res = models.Book.objects.annotate(author_num=Count('author')).filter(author_num__gt=1).values('title','author_num')
print(res)
# 统计各个作者的图书总价格 作者名字 价格
res = models.Author.object.annotate(su=Sum('book_price')).values('name', 'su')

Point values ​​between the field you want to group

res = models.Author.object.values('title')annotate(su=Sum('book_price')).values('name', 'su')

F and Q query 1-3

# 模块导入
from django.db.models import F
# 1 查询库存数大于卖出数的书籍
res = models.Book.objects.filter(kun_cun__gt=F('mai_chu')).values('title')
# 后面的条件是来自于数据库的其他字段

# 2 将所有书的价格上加100元
res = models.Book.objects.all().update(price=F('price') + 100)

# 3 所有书的书名后添加 爆款
res = models.Book.objects.all().update(price=Concat(F('price') + '爆款'))
# 字符串必须使用Concat 了解知识点
# 模块导入
from django.db.models import Q
# 查询一下书籍名称是三国演艺 或者 库存是500的书籍
# filter 内只能是 and关系
res = models.Book.objects.filter(Q(tilte='三国演义'), Q(kun_cun=500)) # , 号还是 and
res = models.Book.objects.filter(Q(tilte='三国演义')|Q(kun_cun=500)) # | 就是 or
res = models.Book.objects.filter(`Q(tilte='三国演义')|Q(kun_cun=500)) # not 关系 取反 esc下面的波浪

Q Objects Advanced Usage

# 类似于搜索功能
q = Q() # 生成对象
q.children.append(('title', '三国演义')) # 一定要是元组
q.children.append(('kun_cun', 500))
res = models.Book.objects.filter(q) # 默认是 and关系
res = models.Book.objects.filter(`q) # 取反
# 方法 
q.connector = 'or' # 指定为or (*******)

Guess you like

Origin www.cnblogs.com/lddraon1/p/11948986.html