1 分组查询
# 分组查询 # 查询每一个出版社id,以及图书平均价格(单表) # 原生sql # select publish_id,avg(price) from app01_book group by publish_id; # orm 实现 """标准 annotate() 内写聚合函数 QuerySet对象的方法 values在前,表示group by 的字段 values在后,表示取字段 filter在前,表示where条件 filter在后,表示having 分组之后 """ from django.db.models import Avg,Sum,Count,Max # res = models.Book.objects.all().values('publish_id').annotate(price_avg=Avg('price')).values('publish_id','price_avg') # print(res) # 查询出版社id大于1的出版社id,以及出书平均价格 # res = models.Book.objects.values('publish_id').filter(publish_id__gt=1).annotate(price_avg=Avg('price')).values('publish_id','price_avg') # print(res) # 查询出版社id大于1的出版社id,以及出书平均价格大于30的 # res = models.Book.objects.values('publish_id').filter(publish_id__gt=1).annotate(price_avg=Avg('price')).filter(price_avg__gt=30).values('publish_id','price_avg') # print(res) # 查询每一个出版社出版的名称和书籍个数(连表) # 连表的话最好以group by的表作为主表 # 最后一个values只能取Publish表中的字段和annotate中的字段 # res = models.Publish.objects.values('nid').annotate(book_count=Count('book__nid')).values('name','book_count') # 简写 如果基表是group by 的表,就可以不写values # res = models.Publish.objects.annotate(book_count=Count('book')).values('name','book_count') # 以book为基表 # res = models.Book.objects.values('publish__nid').annotate(book_count=Count('nid')).values('publish__name','book_count') # print(res) # 查询每个作者的名字,以及出版过书籍的最高价格(建议使用分组的表作为基表) # 多对多如果不以分组表为基表,可能会出数据问题 # res = models.Author.objects.annotate(price_max=Max('book__price')).values('name','price_max') # res = models.Book.objects.values('authors__nid').annotate(price_max=Max('price')).values('authors__name','price_max') # print(res) # 查询每一个书籍的名称,以及对应的作者个数 # res = models.Book.objects.annotate(author_count=Count('authors__name')).values('name','author_count') # res = models.Book.objects.annotate(author_count=Count('authors')).values('name','author_count') # print(res) # 统计不止一个作者的图书 # res = models.Book.objects.annotate(author_count=Count('authors')).filter(author_count__gt=1).values('name','author_count') # print(res) # 统计价格数大于10元,作者的图书 # res = models.Book.objects.values('price').filter(price__gt=10).annotate(author_count=Count('authors')).values('name','author_count') # res = models.Book.objects.filter(price__gt=10).annotate(author_count=Count('authors')).values('name','author_count') # print(res) # 统计价格数大于10元,作者个数大于1的图书 # res = models.Book.objects.values('price').filter(price__gt=10).annotate(author_count=Count('authors')).filter(author_count__gt=1).values('name') # res = models.Book.objects.filter(price__gt=10).annotate(author_count=Count('authors')).filter(author_count__gt=1).values('name','price','author_count') # print(res)2 图书管理系统项目
1 后端是django+mysql/sqlite2 前端:jquery,bootstrap3 首页,图书列表展示,图书新增,修改,作者展示,新增,修改,出版社展示,新增,修改...4 项目地址:https://gitee.com/liuqingzheng/books补充
1 wsgi, uwsgi, cgi, fastcgi
浏览器只能发出HTTP的请求本质上:浏览器是一个socket客户端,服务器是一个socket服务端web服务器性能的高低决定了整个项目性能的高低请求对象environment 字典 =》 request对象=》Django框架()start_response响应对象python:有wsgi协议,uwsgi,gunicornjava:Tomcat,Jbossphp:php服务器wsgi:协议,规定了如何拆HTTP请求,拆到一个python字典中,environment,响应对象,start_responsewsgiref:符合wsgi协议的web服务器CGI:通用网关接口。一句话总结:一个标准,定义了客户端服务器之间如何传数据FastCGI:快速通用网关接口。一句话总结:CGI的升级版WSGI:Web服务器网关接口。一句话总结:为python定义的web服务器和web框架之间的接口标准uWSGI:用c语言写的,性能比较高 一句话总结:一个Web Server,即一个实现了WSGI的服务器,大体和Apache是一个类型的东西,处理发来的请求uwsgi:是一种通信协议 uWSGI自有的协议详情见:http://liuqingzheng.top/article/1/05-CGI,FastCGI,WSGI,uWSGI,uwsgi%E4%B8%80%E6%96%87%E6%90%9E%E6%87%82/ # asgi协议 异步服务网关接口回顾
1 分组查询 -把同一类归为一组,然后使用聚合函数操作 -如果是多表,把两个表连起来,再分组,再聚合 -取得字段必须是分组字段或者聚合函数的字段 如果是单表只能取分组字段和聚合函数聚合的字段 如果是多表可以取分组表中的所有字段和聚合函数的字段 -总结: -annotate(聚合函数) -values在前,表示分组字段 -values在后,表示取字段 -filter在前,表示where条件 -filter在后,表示having条件2 wsgi,uWSGI,uwsgi,cgi,fastcgi3 前后端开发模式 -动态网站(网页的内容是由数据库渲染过来的 每次刷新都不一样)和静态网站(一个死页面 不变) -前后端分离:后端只写后端,返回json格式字符串,js语言 DOM vue react -前后端混合开发:模板,dtl(模板语法 本质就是字符串的替换),jsp,php 4 图书管理系统 -后端是Django+mysql+bootstrap (主机管理系统,人事管理系统,文档分享平台) -图书增删查改 -增,删,查 -出版社的增删查改 -作者的增删查改 页面静态化