bookmanager库的针对表的各种查询操作

项目1:单表查询
1、 查询所有图书的基本信息。
方法一:
select *
from book
方法二:
select 图书编号,图书名称,作者,出版社编号,版次,价格,库存数量
from book
bookmanager库的针对表的各种查询操作

2、查询所有图书的图书编号、图书名称和价格。
select 图书编号,图书名称,价格
from book
bookmanager库的针对表的各种查询操作

3、查询部门为计算机系的读者的借书证号、姓名和联系电话。
select 借书证号,姓名,联系电话
from reader
where 部门=‘计算机系’
bookmanager库的针对表的各种查询操作

4、查询借书证号为R00001且所借图书编号为B00003的借阅日期。
select 借阅日期
from borrow
where 借书证号=‘R00001’ and 图书编号=‘B00003’

bookmanager库的针对表的各种查询操作

5、查询姓刘读者的基本信息。
select *
from reader
where 姓名 like ‘刘%’
bookmanager库的针对表的各种查询操作

6、查01询borrow表中未还图书的记录。
select *
from borrow
where 还书日期 is null
bookmanager库的针对表的各种查询操作

7、查询借阅日期在“2008-12-6”至“2009-2-6”的借书记录信息。
方法一:
select *
from borrow
where 借阅日期 between '2008-12-6’and ‘2009-2-6’
方法二:
select *
from borrow
where 借阅日期 >=‘2008-12-6’ AND 借阅日期<=‘2009-2-6’
bookmanager库的针对表的各种查询操作

项目2:对查询结果进行编辑
1、查询reader表中读者姓名和部门,输出结果的字段名分别用英文:name和department。
select 姓名 as name,部门 as department
from reader
bookmanager库的针对表的各种查询操作

2、查询借书证号为R00001的读者所借图书至今的已有多少天。
select datediff (dd, 借阅日期,getdate())
from borrow
where 借书证号='r0
bookmanager库的针对表的各种查询操作

3、查11询借书证号为R00002的读者一共借了多少本图书。
select count(*) 借阅册数
from borrow
where 借书证号=‘R00002’
bookmanager库的针对表的各种查询操作

4、查询所有图书信息,结果按价格的降序排列。
select *
from book
order by 价格 desc
bookmanager库的针对表的各种查询操作

5、统计图书信息表中不同出版社出版的图书的数目,把统计结果大于或等于2的记录输出。
select 出版社编号 ,count() 出版图书的数目
from book
group by 出版社编号
having count(
)>=2
bookmanager库的针对表的各种查询操作

6、查询所有图书的库存总量。
select sum(库存数量) 库存总量
from book
bookmanager库的针对表的各种查询操作

7、根据借阅日期和借书期限,以及现在的日期,判断还书日期为空的借阅记录是否过期,输出过期的借阅记录。
select *
from borrow
where 还书日期 is null and datediff(dd,借阅日期,getdate())>借书期限

bookmanager库的针对表的各种查询操作

8、根据借阅日期和借书期限,以及现在的日期,判断还书日期为空的借阅记录是否过期,若过期,请把“是否过期”属性的值修改为“是”。
update borrow
set 是否过期='是 ’
where 还书日期 is null and datediff(dd,借阅日期,getdate())>借书期限

项目3:连接查询
1、查询读者的借书证号、姓名、联系电话以及所借书的图书编号、借阅日期。
select reader.借书证号,姓名,联系电话,图书编号,借阅日期
from reader,borrow
where reader.借书证号=borrow.借书证号
bookmanager库的针对表的各种查询操作

2、查询图书信息以及相对应的出版社的详细信息。
select *
from book,publish
where book.出版社编号=publish.出版社编号
bookmanager库的针对表的各种查询操作

*3、查询读者的借书证号、姓名、部门以及所借图书的图书编号、借阅日期,没有连接成功的记录也要输出。

*4、查询借阅信息表中读者的借书证号、图书编号、借阅日期以及所借图书在图书信息表中对应的出版社编号,图书信息表中没连接成功的记录也要输出。

*5、查询读者的借书证号、姓名、联系电话以及所借书的图书编号、借阅日期,没有借阅记录的读者信息也要输出。

6、查询读者的借书证号、姓名、联系电话以及所借图书的图书编号、借阅日期、出版社编号和名称。
select reader.借书证号,姓名,reader.联系电话,borrow.图书编号,publish.出版社编号,借阅日期
from reader,borrow,publish,book
where reader.借书证号=borrow.借书证号 and book.图书编号=borrow.图书编号 and book.出版社编号=publish.出版社编号
bookmanager库的针对表的各种查询操作
项目4:子查询
1、查询借阅了图书编号为B00001的图书的读者信息。
条件:借阅了图书编号为B00001的图书的读者信息-borrow
结果:读者信息–reade
方法一
select reader.*
from reader,borrow
where reader.借书证号=borrow.借书证号
and 图书编号=‘B00001’
方法二
select *
from reader
where 借书证号 in
(select 借书证号
from borrow
where 图书编号=‘B00001’)

bookmanager库的针对表的各种查询操作

2、查询借阅了高等教育出版社出版和清华大学出版社出版的图书的读者信息。
方法一:多表等值
select distinct reader.*
from publish,book,borrow,reader
where publish.出版社编号 =book.出版社编号
and book.图书编号 =borrow.图书编号
and borrow.借书证号 =reader.借书证号
and (出版社名称=‘高等教育出版社’ or 出版社名称=‘清华大学出版社’)
方法二、
select distinct reader.*
from publish,reader,book,borrow
where publish.出版社编号=book.出版社编号
and book.图书编号=borrow.图书编号
and borrow.借书证号=reader.借书证号
and 出版社名称 in (‘高等教育出版社’ ,‘清华大学出版社’)
方法三、
select*
from reader
where 借书证号 in
(select 借书证号
from borrow
where 图书编号 in
( select 图书编号
from book
where 出版社编号 in
(select 出版社编号
from publish
where 出版社名称 in (‘高等教育出版社’ ,‘清华大学出版社’))))

bookmanager库的针对表的各种查询操作

3、查询未按时归还图书的读者的借书证号、姓名、部门和联系电话。
条件:未按时归还图书的读者–borrow
结果:读者的借书证号、姓名、部门和联系电话。–reade
方法一
select distinct reader.借书证号,reader.姓名,reader.部门
from reader,borrow
where reader.借书证号=borrow.借书证号
and 还书日期 is null
and datediff(dd,借阅日期,getdate())>借书期限
方法二
select 借书证号,姓名,部门,联系电话
from reader
where 借书证号 in
(select 借书证号
from borrow
where 还书日期 is null
and datediff(dd,借阅日期,getdate())>借书期限 )
bookmanager库的针对表的各种查询操作

4、查询姓名为张云的读者的借阅记录。
条件:姓名为张云的读者
结果:借阅记录
select borrow.*
from reader,borrow
where reader.借书证号=borrow.借书证号
and 姓名=‘张云’

select *
from borrow
where 借书证号 in
(select 借书证号
from reader
where 姓名=‘张云’)
bookmanager库的针对表的各种查询操作

5、查询姓名为张云的读者所借阅图书的详细信息。
条件:姓名为张云的读者–reader
结果:读者所借阅图书的详细信息–book
reader-borrow-book
方法一:多表等值
select book.*
from reader,borrow,book
where reader.借书证号=borrow.借书证号
and borrow.图书编号=book.图书编号
and 姓名=‘张云’
方法二:子查询
select *
from book
where 图书编号 in
(select 图书编号
from borrow
where 借书证号 in
(select 借书证号
from reader
where 姓名=‘张云’))
方法三:混合式
select *
from book
where 图书编号 in
(select 图书编号
from borrow,reader
where borrow.借书证号=reader.借书证号
and 姓名=‘张云’)

bookmanager库的针对表的各种查询操作

6、查询吴杉杉是否有借阅图书的记录,若有则输出借阅记录。
条件:吴杉杉是否有借阅图书的记录–reader
结果:借阅记录–borrow
方法一:多表等值
select borrow.*
from reader,borrow
where reader.借书证号=borrow.借书证号
and 姓名=‘吴杉杉’
方法二:
select borrow.*
from borrow
where 借书证号 in
(select 借书证号
from reader
where 姓名=‘吴杉杉’)

bookmanager库的针对表的各种查询操作

7、查询图书价格比所有图书平均价格高的图书信息。

select *
from book
where 价格>
		(select avg(价格)
		 from book)

bookmanager库的针对表的各种查询操作
8、查询图书价格比图书编号为B00003和B00004的价格都高的图书信息。
select *
from book
where 价格>all
(select 价格
from book
where 图书编号 in(‘B00003’,‘B00004’))
bookmanager库的针对表的各种查询操作

上一篇:评测三款最流行的txt阅读器


下一篇:mybatis-plus queryWrapper如何舍弃null值