数据库实验之《表的连接和嵌套查询》

表的连接&嵌套查询

实验目的

  • 熟练掌握Mysql查询方法,加深对SQL语言查询语句的理解。
  • 掌握多表的连接查询与嵌套查询语句的正确写法和实验操作方法。

实验环境

  • Windows10,MySQL Workbench 8.0 CE
  • 准备数据:book、reader、borrowed表

book表(书本编号,类别,出版社,书名,作者,数量,单价,购买日期,备注)

book

reader表(读者编号,读者姓名,学院,性别,电话)

在这里插入图片描述

borrow表(书本编号,读者编号,借阅日期时间,归还日期时间)

在这里插入图片描述

实验内容

  1. 查询借阅了类别为 “信息技术” 类图书的所有读者编号、姓名及单位
select rno, rname, depart
	from reader
where rno in (select rno
		from borrow
	where bno in (select bno 
			from book
		where bclass = '信息技术')
);
  1. 查询借阅过图书的读者编号、姓名及单位
select rno, rname, depart
	from reader
where rno in (select rno
		from borrow);
  1. 查询姓名为 “章小鱼” 的读者目前借阅的图书书号和书名
select bno, bname
	from book
where bno in (select bno 
		from borrow
	where rno = (select rno
			from reader
		where rname = '章小鱼'
	)
);
  1. 查询借书过期的所有读者姓名及所借图书名(假定借阅期为60天)
select * 
	from borrow
where  reb_date is null and
	   datediff(curdate(), date(bor_date)) > 60;

注:
datediff (A,B) 函数返回 A-B 的天数(day),其中A、B的格式为日期 (年-月-日)
curdate ( ) 函数返回当前日期 (year-month-day)
date(C) 函数返回C的日期,其中C的格式为日期时间 (y-m-d h-m-s)

  1. 查询没有借阅过图书的所有读者姓名
select rname
	from reader
where rno not in (
	select rno 
		from borrow
);
  1. 查询借出次数超过10次的所有图书的书号和书名
select bno, bname
	from book
where  bno in (
	select bno 
		from borrow
	group by bno 
		having  count(rno) > 10
);
  1. 查询除已还的书以外,目前借了5本或以上图书的读者编号和姓名
select rno,rname
	from reader
where rno in (
	select rno
		from borrow
	group by rno 
		having count(bno) >= 5
);
  1. 查询书库中除借出的图书外,现存的图书书号、书名、现存数量
-- 这题难在如何把两个表的相同属性列相减,
-- 这里用到了“虚表”的方法;
-- 此题借鉴了不少博主的方法,链接放在文末
select book.bno as '图书编号',
	   bname as '书名',
	   (book.bnum-a.borrowed) as '现存数量'
	from book,(select bno,count(*) as borrowed
				from borrow
				group by bno) as a
where book.bno=a.bno;

实验小结

  • 本实验主要练习了表的连接查询和嵌套查询,为了满足题目要求,我更新了数据表的一些内容,对数据库的进行相应的增、删、改。
  • 其中后面几道题结构比较复杂,我花了相当一部分时间理解语句意思,查询相关资料,最后都如期解决了。

参考链接

SQL完整源码

use book_reader_db;

select bno 
	from book
where bclass = '信息技术';

select rno
	from borrow
where bno in (select bno 
		from book
	where bclass = '信息技术');
    
-- 查询借阅了类别为“信息技术“类图书的所有读者编号、姓名及单位
select rno, rname, depart
	from reader
where rno in (select rno
		from borrow
	where bno in (select bno 
			from book
		where bclass = '信息技术')
);

-- 查询借阅过图书的读者编号、姓名及单位
select rno, rname, depart
	from reader
where rno in (select rno
		from borrow);

-- 查询姓名为“章小鱼”的读者目前借阅的图书书号和书名
select bno, bname
	from book
where bno in (select bno 
		from borrow
	where rno = (select rno
			from reader
		where rname = '章小鱼'
	)
);

-- 查询借书过期的所有读者姓名及所借图书名
--(假定借阅期为60天)
select * 
	from borrow
where  reb_date is null and
	   datediff(curdate(), date(bor_date)) > 60;
       
--查询没有借阅过图书的所有读者姓名
--(之前的用户都借过书,故先插入一个未借书的图同学)
insert into reader 
	values(20180417,'卢小冰','男','软件学院',13779084265);

select rname
	from reader
where rno not in (
	select rno 
		from borrow
);

select * from reader;

--查询借出次数超过10次的所有图书的书号和书名
--(同样,先在reader标签下插入一些数据)
insert into borrow values(1003,20180101,'2020-02-15','2020-05-15');
insert into borrow values(1003,20180102,'2020-02-16',null);
insert into borrow values(1003,20180103,'2020-02-17','2020-04-25');
insert into borrow values(1003,20180201,'2020-03-01',null);
insert into borrow values(1003,20180202,'2020-03-02','2020-04-15');
insert into borrow values(1003,20180203,'2020-03-03',null);
insert into borrow values(1003,20180301,'2020-04-14','2020-05-01');
insert into borrow values(1003,20180302,'2020-04-15',null);
insert into borrow values(1001,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1002,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1003,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1004,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1005,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1006,20180303,'2020-04-16','2020-05-15');
insert into borrow values(1003,20180518,'2020-02-22',null);
insert into borrow values(1003,20180417,'2020-02-22',null);
insert into borrow values(1003,20180428,'2020-02-22',null);

select bno 
	from borrow
group by bno 
	 having  count(rno) > 10;


select bno, bname
	from book
where  bno in (
	select bno 
		from borrow
	group by bno 
		having  count(rno) > 10
);

-- 查询除已还的书,目前借了5本或以上图书的读者编号和姓名
select rno,count(bno)
	from borrow
group by rno 
	having count(bno) >= 5;

select rno,rname
	from reader
where rno in (
	select rno
		from borrow
	group by rno 
		having count(bno) >= 5
);

-- 查询书库中除借出的图书外,现存的图书书号、书名、现存数量
select bno,count(*)
	from borrow
group by bno;

	-- 创建虚表borrowed进行连接运算
select book.bno as '图书书号',
	   bname as '书名',
	   (book.bnum-a.borrowed) as '现存数量'
	from book,(select bno,count(*) as borrowed
				from borrow
				group by bno) as a
where book.bno=a.bno;

猜你喜欢

转载自blog.csdn.net/weixin_43445874/article/details/106226111