数据库原理及应用(MySQL版)——实验三 MySQL查询 - Go语言中文社区

数据库原理及应用(MySQL版)——实验三 MySQL查询


1、实验目的

  1. 熟悉SELECT语句的语法结构;
  2. 熟练掌握组合使用where、group by、having和order by等子句;
  3. 熟悉聚合函数的使用;
  4. 熟悉子查询、多表查询的使用。

2、实验内容及要求

设有一个图书馆数据库,其中包括三个表,即图书表、读者表和借阅表。三个表的结构和数据如下:
图书表book(书号bno,书名bname,作者author,出版社publish,单价price)
请添加图片描述

读者表reader(读者号rno,姓名rname,性别sex,电话tel,部门department,地址address)
请添加图片描述

借阅表borrow(读者号rno,书号bno,借出日期bdate,归还日期rdate)

请添加图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

完成下列问题的SQL语句,并截图。

  1. 查询读者表中的所有读者信息。
select *from reader;
  1. 查询张姓读者的读者号、姓名及部门。
select rno,rname,department from reader where rname like '张%';
  1. 查询信息工程学院学生的姓名、性别、部门,列标题分别显示为“姓名”、“性别”、“部门”。
select rname as '姓名',sex as '性别', department as '部门' from reader where department='信息工程学院';
  1. 查询学生表中男生总人数和女生总人数。
select sex as '性别',count(sex) from reader group by sex;
  1. 查询借阅“数据库原理”的读者的姓名和部门。
select reader.rname,reader.department from reader,book,borrow where reader.rno=borrow.rno and book.bno=borrow.bno and book.bname like '数据库原理' ;
  1. 查询所有单价不在20-30元之间的图书信息。
select * from book where price not between 20 and 30;
  1. 查询和“谢爽”借了同一本书的读者的读者号、姓名、部门。
select  distinct reader.rno 读者号,rname 姓名,department 部门
from reader,borrow where reader.rno=borrow.rno and
bno in(Select bno from borrow 
where rno in(select rno from reader 
where rname="谢爽")) and rname !="谢爽";
  1. 查询姓名的第二个字符是‘志’并且名字是三个字的读者的读者号及姓名。
select rno,rname from reader where rname like  '_志_';
select rno,rname from reader where rname regexp '.志.';
  1. 查询既不是机械工业出版社也不是科学出版社出版的图书信息。
select * from book where publish not in  ('机械工业出版社','科学出版社');
  1. 查找姓名以王、张、或李开头的所有读者的读者号及姓名
select rno,rname from reader where rname like '王%' or rname like'张%' or rname like'李%';
  1. 查询借阅图书数超过2本的读者号、总本书,并按照借阅本书从大到小排序。
select rno as '读者号',count(bno) as '总本数'
from borrow
group by rno
having count(bno)>2
order by count(bno) desc;
  1. 查询读者的读者号、姓名、借阅的图书名、借出日期和归还日期,按借书时间由早到晚排序。
select reader.rno 读者号,rname 姓名,bname 图书名,bdate 借出日期,rdate 归还日期
from reader,book,borrow 
where book.bno=borrow.bno and reader.rno=borrow.rno
order by bdate;
  1. 查询借阅了清华大学出版社,并且书名包含‘数据’这个字的图书的读者,并显示读者号、姓名、书名、出版社、借出日期及归还日期。
select reader.rno 读者号,rname 姓名,bname 书名,publish 出版社,bdate 借出日期,rdate 归还日期
from borrow join book on book.bno=borrow.bno
join reader on borrow.rno=reader.rno
where publish='清华大学出版社' and bname like '%数据%';
  1. 查询所有单价小于平均单价的图书的书号、书名及出版社。
select bno 书号,bname 书名,publish 出版社
from book 
where price<(select avg(price) from book);
  1. 查询已经被借阅过并已经归还的图书信息。
select * from book
where bno in (select bno 
from borrow
where bdate is not null and rdate is not null);
  1. 查询从未被借阅过的图书信息。
select*from book
where bno not in (select bno from borrow );
  1. 查询正在被借阅的图书信息。
select*from book
where bno in (select bno from borrow where rdate is null );
  1. 查询借阅图书总数最多的部门。
select department "借书最多部门",count(borrow.rno) "借阅图书总数"
from reader,borrow 
where reader.rno=borrow.rno
group by department
order by  count(department) desc limit 2;
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_45280880/article/details/121378677
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-03 19:23:20
  • 阅读 ( 367 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢