1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 数据库原理及应用(MySQL版)——实验三 MySQL查询

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

时间:2022-01-01 12:57:01

相关推荐

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

实验三 MySQL查询

1、实验目的2、实验内容及要求

1、实验目的

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

2、实验内容及要求

设有一个图书馆数据库,其中包括三个表,即图书表、读者表和借阅表。三个表的结构和数据如下:

图书表book(书号bno,书名bname,作者author,出版社publish,单价price)

读者表reader(读者号rno,姓名rname,性别sex,电话tel,部门department,地址address)

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

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

查询读者表中的所有读者信息。

select *from reader;

查询张姓读者的读者号、姓名及部门。

select rno,rname,department from reader where rname like '张%';

查询信息工程学院学生的姓名、性别、部门,列标题分别显示为“姓名”、“性别”、“部门”。

select rname as '姓名',sex as '性别', department as '部门' from reader where department='信息工程学院';

查询学生表中男生总人数和女生总人数。

select sex as '性别',count(sex) from reader group by sex;

查询借阅“数据库原理”的读者的姓名和部门。

select reader.rname,reader.department from reader,book,borrow where reader.rno=borrow.rno and book.bno=borrow.bno and book.bname like '数据库原理' ;

查询所有单价不在20-30元之间的图书信息。

select * from book where price not between 20 and 30;

查询和“谢爽”借了同一本书的读者的读者号、姓名、部门。

select distinct reader.rno 读者号,rname 姓名,department 部门from reader,borrow where reader.rno=borrow.rno andbno in(Select bno from borrow where rno in(select rno from reader where rname="谢爽")) and rname !="谢爽";

查询姓名的第二个字符是‘志’并且名字是三个字的读者的读者号及姓名。

select rno,rname from reader where rname like '_志_';select rno,rname from reader where rname regexp '.志.';

查询既不是机械工业出版社也不是科学出版社出版的图书信息。

select * from book where publish not in ('机械工业出版社','科学出版社');

查找姓名以王、张、或李开头的所有读者的读者号及姓名

select rno,rname from reader where rname like '王%' or rname like'张%' or rname like'李%';

查询借阅图书数超过2本的读者号、总本书,并按照借阅本书从大到小排序。

select rno as '读者号',count(bno) as '总本数'from borrowgroup by rnohaving count(bno)>2order by count(bno) desc;

查询读者的读者号、姓名、借阅的图书名、借出日期和归还日期,按借书时间由早到晚排序。

select reader.rno 读者号,rname 姓名,bname 图书名,bdate 借出日期,rdate 归还日期from reader,book,borrow where book.bno=borrow.bno and reader.rno=borrow.rnoorder by bdate;

查询借阅了清华大学出版社,并且书名包含‘数据’这个字的图书的读者,并显示读者号、姓名、书名、出版社、借出日期及归还日期。

select reader.rno 读者号,rname 姓名,bname 书名,publish 出版社,bdate 借出日期,rdate 归还日期from borrow join book on book.bno=borrow.bnojoin reader on borrow.rno=reader.rnowhere publish='清华大学出版社' and bname like '%数据%';

查询所有单价小于平均单价的图书的书号、书名及出版社。

select bno 书号,bname 书名,publish 出版社from book where price<(select avg(price) from book);

查询已经被借阅过并已经归还的图书信息。

select * from bookwhere bno in (select bno from borrowwhere bdate is not null and rdate is not null);

查询从未被借阅过的图书信息。

select*from bookwhere bno not in (select bno from borrow );

查询正在被借阅的图书信息。

select*from bookwhere bno in (select bno from borrow where rdate is null );

查询借阅图书总数最多的部门。

select department "借书最多部门",count(borrow.rno) "借阅图书总数"from reader,borrow where reader.rno=borrow.rnogroup by departmentorder by count(department) desc limit 2;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。