day62作业

爷,独闯天下 提交于 2020-01-13 05:36:48
import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_ORM.settings")import djangodjango.setup()from app01 import models# 1 .查找所有书名里包含金老板的书# ret1 = models.Book.objects.filter(title__contains="金老板")# print(ret1)"""select * from app01_book where title regexp "金老板";"""# 2.查找出版日期是2018年的书# ret2 = models.Book.objects.filter(publish_date__year=2018)# print(ret2)"""select * from app01_book where year(publish_date)=2018;"""# 3.查找出版日期是2017年的书名# ret3 = models.Book.objects.filter(publish_date__year=2017)# for i in ret3:#     print(i.title)"""select title from app01_book where year(publish_date)=2017;"""# 4.查找价格大于10元的书# ret4 = models.Book.objects.filter(price__gt=10)# print(ret4)"""select * from app01_book where price>10;"""# 5.查找价格大于10元的书名和价格# ret5 = models.Book.objects.filter(price__gt=10)# for i in ret5:#     print(i.title,i.price)"""select title,price from app01_book where price>10;"""# 6.查找memo字段是空的书# ret6 = models.Book.objects.filter(memo__isnull=True)# print(ret6)"""select * from app01_book where memo is Null;"""# 7.查找在北京的出版社# ret7 = models.Publisher.objects.filter(city="北京")# print(ret7)"""select * from app01_publisher where city = "北京";"""# 8.查找名字以沙河开头的出版社# ret8 = models.Publisher.objects.filter(name__startswith="沙河")# print(ret8)"""select * from app01_publisher where name like "沙河%";"""# 9.查找“沙河出版社”出版的所有书籍# ret = models.Publisher.objects.filter(name="沙河出版社")# ret9 = []# for i in ret:#     ret9.extend(models.Book.objects.filter(publisher=i.pk))# print(ret9)"""select * from app01_book where publisher_id in (select app01_publisher.id from app01_publisher where name = "沙河出版社");"""# 10.查找每个出版社出版价格最高的书籍价格# publisher_list = models.Publisher.objects.all()# publisher_id_list = []# for i in publisher_list:#     publisher_id_list.append(i.pk)# max_list = []# for i in publisher_id_list:#     ret = models.Book.objects.filter(publisher=i)#     price_list = []#     for j in ret:#         price_list.append(j.price)#     price_list.sort(reverse=True)#     if price_list:#         max_list.append(price_list[0])#     else:#         max_list.append("无出版书籍")# print(max_list)"""select app01_publisher.name,max(price) from app01_book inner join app01_publisher  on app01_book.publisher_id = app01_publisher.id group by publisher_id;"""# 11.查找每个出版社的名字以及出的书籍数量# publisher_list = models.Publisher.objects.all()# publisher_info_list = []# for i in publisher_list:#     publisher_info_list.append({i.pk:i.name})# pub_name_and_books_number = []# for i in publisher_info_list:#     book_count = models.Book.objects.filter(publisher=list(i.keys())[0]).count()#     pub_name_and_books_number.append({list(i.values())[0]:book_count})# print(pub_name_and_books_number)"""select app01_publisher.name,count(*) from app01_book inner join app01_publisher  on app01_book.publisher_id = app01_publisher.id group by publisher_id;"""# 12.查找作者名字里面带“小”字的作者# ret12 = models.Author.objects.filter(name__contains="小")# print(ret12)"""select * from app01_author where name regexp "小";"""# 13.查找年龄大于30岁的作者# ret13 = models.Author.objects.filter(age__gt=30)# print(ret13)"""select * from app01_author where age>30;"""# 14.查找手机号是155开头的作者# ret14 = models.Author.objects.filter(phone__startswith="155")# print(ret14)"""select * from app01_author where phone like "155%";"""# 15.查找手机号是155开头的作者的姓名和年龄# ret14 = models.Author.objects.filter(phone__startswith="155")# for i in ret14:#     print(i.name,i.age)"""select name,age from app01_author where phone like "155%";"""# 16.查找每个作者写的价格最高的书籍价格# author_list = models.Author.objects.all()# for i in author_list:#     book = i.book_set.all().order_by("price").last()#     print(i.name,book.price)"""select a01a.name,max(price) from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id group by a01a.id;"""# 17.查找每个作者的姓名以及出的书籍数量# author_list = models.Author.objects.all()# for i in author_list:#     book_count = i.book_set.all().count()#     print(i.name,book_count)"""select a01a.name,count(*) from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id group by a01a.id;"""# 18.查找书名是“跟金老板学开车”的书的出版社# book = models.Book.objects.filter(title="跟金老板学开车")[0]# publisher = models.Publisher.objects.filter(pk=book.publisher.pk)# print(publisher)"""select * from app01_book inner join app01_publisher a01p on app01_book.publisher_id = a01p.id where title="跟金老板学开车";"""# 19.查找书名是“跟金老板学开车”的书的出版社所在的城市# book = models.Book.objects.filter(title="跟金老板学开车")[0]# publisher = models.Publisher.objects.filter(pk=book.publisher.pk)# print(publisher[0].city)"""select a01p.city from app01_book inner join app01_publisher a01p on app01_book.publisher_id = a01p.id where title="跟金老板学开车";"""# 20.查找书名是“跟金老板学开车”的书的出版社的名称# book = models.Book.objects.filter(title="跟金老板学开车")[0]# publisher = models.Publisher.objects.filter(pk=book.publisher.pk)# print(publisher[0].name)"""select a01p.name from app01_book inner join app01_publisher a01p on app01_book.publisher_id = a01p.id where title="跟金老板学开车";"""# 21.查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格#book = models.Book.objects.filter(title="跟金老板学开车")[0]# orther_books = models.Book.objects.filter(publisher=book.publisher.pk)# for i in orther_books:#     print(i.title,i.price)"""select title,price from app01_book where publisher_id =( select a01p.id from app01_book inner join app01_publisher a01p on app01_book.publisher_id = a01p.id where title="跟金老板学开车");"""# 22.查找书名是“跟金老板学开车”的书的所有作者# book = models.Book.objects.filter(title="跟金老板学开车")[0]# for  i in book.author.all():#     print(i.name)"""select a01a.name from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id where title= "跟金老板学开车";"""# 23.查找书名是“跟金老板学开车”的书的作者的年龄# book = models.Book.objects.filter(title="跟金老板学开车")[0]# for  i in book.author.all():#     print(i.age)"""select a01a.age from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id where title= "跟金老板学开车";"""# 24.查找书名是“跟金老板学开车”的书的作者的手机号码# book = models.Book.objects.filter(title="跟金老板学开车")[0]# for  i in book.author.all():#     print(i.phone)"""select a01a.phone from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id where title= "跟金老板学开车";"""# 25.查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱# book = models.Book.objects.filter(title="跟金老板学开车")[0]# for i in book.author.all():#     for j in i.book_set.all():#         print(i.name,j.title,j.price)"""select a01a.name,title,price from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id where a01a.id in (select a01a.id from app01_book inner join app01_book_author a01ba on app01_book.id = a01ba.book_id inner join app01_author a01a on a01ba.author_id = a01a.id where title= "跟金老板学开车");"""
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!