一: 多表练习查询
1、 自行创建测试数据;
2、 查询学生总人数;
3、 查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
4、 查询每个年级的班级数,取出班级数最多的前三个年级;
5、 查询平均成绩最高的学生的id和姓名以及平均成绩;
6、 查询每个年级的学生人数;
7、 查询每位学生的学号,姓名,平均成绩;
8、 查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名及分数;
9、 查询姓“李”的老师的个数和所带班级数;
10、查询班级数小于5的年级id和年级名;
11、查询教过课程超过2门的老师的id和姓名;
12、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
13、查询所带班级数最多的老师id和姓名;
14、查询有课程成绩小于60分的同学的学号、姓名;
15、查询男生、女生的人数,按倒序排列;
16、查询各个课程及相应的选修人数;
17、查询同时选修了物理课和生物课的学生id和姓名;
18、检索“3”课程分数小于60,按分数降序排列的同学学号;
19、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
20、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
二:
表关系:
from django.shortcuts import render, HttpResponse, reverse
from django.urls import reverse
from app01 import models
# Create your views here.
def show(request):
# 一对多 创建
# 方式一 传对象
# pub_obj =models.Publish.objects.filter(id=1).first()
# print(pub_obj ,type(pub_obj))
# book =models.Book.objects.create(title="swsw",price=280,pub_date="2018-12-21",publish=pub_obj)
# 方式二 传id 创建
# book =models.Book.objects.create(title="chsea",price=221,pub_date="2019-12-28",publish_id=pub_obj.pk)
# 多对多创建
# 方式一
# book_obj =models.Book.objects.filter(id =2).first()
# a1 =models.Author.objects.filter(name="matai").first()
# a2 =models.Author.objects.filter(name="lhc").first()
# book_obj.authors.add(a1,a2)
# book_obj = models.Book.objects.filter(id=3).first()
# a1 = models.Author.objects.filter(name="matai").first()
# a2 = models.Author.objects.filter(name="lhc").first()
# book_obj.authors.add(a1.pk, a2.pk)
# pub_obj =models.Publish.objects.filter(id =2).first()
# print(pub_obj.book_set.remove(),type(pub_obj.book_set))
# book_obj=models.Book.objects.filter(pk=1).first()
# print(book_obj.publish.city)
# pub_obj =models.Publish.objects.filter(id=2).first()
# print(pub_obj.book_set.all(),type(pub_obj.book_set.all()))
# for i in pub_obj.book_set.all():
# print(i.title)
# 一对一查询
# au_obj = models.Author.objects.filter(id=3).first()
# print(au_obj.au_detail.tel)
# au_addr = models.AuthorDetail.objects.filter(id=2).first()
# print(au_addr.author.name)
# 多对多查询
# book_obj =models.Book.objects.filter(id=2).first()
# for i in book_obj.authors.all():
# print(i.name,i.au_detail.tel)
# au_obj =models.Author.objects.filter(id=3).first()
# for j in au_obj.book_set.all():
# print(j.title)
'''1、查询丐帮出版社出版的所有书名和作者名称;
2、查询独孤九剑的作者名称和出版社名称;
3、查询吸星大法的作者的姓名、电话和地址;
4、查询令狐冲写的书籍,在哪些出版社出版过,获取其出版社名称和地址;
5、查询出版社地址为黑木崖,在该出版社出版过书籍的作者姓名和邮箱;
'''
# 正向查询按字段, 反向查询按小写表名。
# 1方式1 正向 双下
# books1 =models.Book.objects.filter(publish__id=2).values("title","authors__name")
# print(books1)
# print(">>>>>>>>>>>>>>>>>")
# # 1方式2 反向 双下
# books2 =models.Publish.objects.filter(id=2).values("book__title","book__authors__name")
# print(books2)
# # 1 方式3 反向
# pub1 =models.Publish.objects.filter(id=2).first()
# for i in pub1.book_set.all():
# for j in i.authors.all():
#
# print(i.title,j.name)
# 2 方法1
# books1 =models.Book.objects.filter(id=2).values("authors__name","publish__name")
# print(books1)
# #2 方法2
# books2 =models.Author.objects.filter(book__id=2).values("name","book__publish__name")
# print(books2)
# #2 方法3
# books3 =models.Book.objects.filter(id=2)
# for i in books3:
# name,pub=i.authors.all(),i.publish.name
# for j in name:
# print(j.name,i.publish)
# 3、查询吸星大法的作者的姓名、电话和地址;
# 3 方法1:
# books1=models.Book.objects.filter(id=3).values("authors__name","authors__au_detail__tel","authors__au_detail__addr")
#
# print(books1)
# #3 方法2:
# books2 =models.Author.objects.filter(book__id=3).values("name","au_detail__tel","au_detail__addr")
# print(books2)
# # 3 方法3:
# books3 =models.Book.objects.filter(id=3).first().authors.all()
# for i in books3:
# print(i.name,i.au_detail.tel,i.au_detail.addr)
# 4、查询令狐冲写的书籍,在哪些出版社出版过,获取其出版社名称和地址;
# 4 方法1:
# books1 =models.Author.objects.filter(id=3).values("book__title","book__publish__name","book__publish__city")
# print(books1)
# # 4 方法2:
# books2 =models.Book.objects.filter(authors__id=3).values("title","publish__name","publish__city")
# print(books2)
# #4 方法3 :
# books3 =models.Author.objects.filter(id=3).first()
# for i in books3.book_set.all():
# print(i.title,i.publish.name,i.publish.city)
#
# 5 5、查询出版社地址为黑木崖,在该出版社出版过书籍的作者姓名和邮箱;
# 5 方法1 :
books1 =models.Publish.objects.filter(id=2).values("book__title","book__authors__name","book__authors__au_detail__addr")
print(books1)
# 5 方法2:
books2 =models.Book.objects.filter(publish_id=2).values("title","authors__name","authors__au_detail__addr")
print(books2)
# 5 方法3:
books3 =models.Publish.objects.filter(id=2).first()
for i in books3.book_set.all():
for j in i.authors.all():
print(i.title,j.name)
return HttpResponse("okok199999999")