单表查询:
假设读者的借阅日期为1个月,查询超时为还的读者编号,图书编号以及超出天数。
【查询语句】:select Rid, Bid , DATEDIFF(day,BorrowDate,ReturnDate)-30 超出天数 from RB
where DATEDIFF(day,BorrowDate,ReturnDate)>30
6)假设读者的借阅日期为1个月,超出每天罚款0.1元,查询超时为还的读者编号,图书编号以及罚款金额。
【查询语句】:select Rid, Bid ,罚金=(DATEDIFF(day,BorrowDate,ReturnDate)-30)*0.1 from RB
where DATEDIFF(day,BorrowDate,ReturnDate)>30
多表查询:
(1)查询借阅了类别为“环境”类图书的所有读者编号、姓名及单位;
【连接查询语句】
--没有环境类图书,改为计算机类;没有单位类,改为系
select distinct RB.Rid, Rname, Rdepartment from RB
inner join Reader on RB.Rid = Reader.Rid
inner join Book on RB.Bid = Book.Bid
where Btype = '计算机'
【嵌套查询语句】
select Rid, Rname, Rdepartment from Reader
where Reader.Rid
in (select RB.Rid from RB inner join Book on RB.Bid=Book.Bid where Btype='计算机')
(2)—查询借阅过图书的读者编号、姓名及单位;
【连接查询语句】
select distinct Reader.Rid, Rname, Rdepartment from Reader
inner join RB on RB.Rid=Reader.Rid
【嵌套查询语句】
select Rid, Rname, Rdepartment from Reader
where Rid in (
select Rid from RB
)
(3)--查询姓名 李明的读者目前借阅的图书书号和书名;
【连接查询语句】--没有李明,改为南宫问天
select Book.Bid, Bname from Book
inner join RB on RB.Bid=Book.Bid
inner join Reader on RB.Rid = Reader.Rid
where Rname='南宫问天'
【嵌套查询语句】
select Bid, Bname from Book
where Bid in (
select Bid from RB inner join Reader on RB.Rid = Reader.Rid
where Rname='南宫问天'
)
(4)查询借书过期的所有读者姓名及所借图书名(假定借阅期为60天);
【连接查询语句】--没有超过60天的,所以假设期限为30天
select Rname,Bname from RB
inner join Reader on RB.Rid=Reader.Rid
inner join Book on RB.Bid=Book.Bid
where DATEDIFF(day,BorrowDate,ReturnDate)>30
(5)查询没有借阅过图书的所有读者姓名;
【连接查询语句】 无
--由于一开始没有相关数据,故临时插入一条。
【嵌套查询语句】
insert Reader values('20170101','轩辕','男','信息学院','99999999955')
select Rname from Reader
where Reader.Rid
not in(select distinct Rid from RB)
(6)查询借出次数超过10次的所有图书的书号和书名;
【连接查询语句】--由于RB表中的记录最多只被借了2次,所以改为超过一次‘
select RB.Bid,Book.Bname from Book
inner join RB on Book.Bid=RB.Bid
group by RB.Bid, Bname
having COUNT(RB.Bid) > 1
【嵌套查询语句】
select Bid,Bname from Book
where Bid in (
select Bid from RB group by Bid having count(Bid) > 1
)
(7)查询除已还的书以外,目前借了5本或以上图书的读者编号和姓名;
【连接查询语句】--由于5本在RB表中没有,所以用1代替
select Rb.Rid,Rname from Reader
inner join RB on Reader.Rid=RB.Rid
where ReturnDate is null
group by RB.Rid,Rname
having count(Bid) >= 1
【嵌套查询语句】
select Rid,Rname from Reader
where Rid in (
select Rid from RB where
Rid in (select Rid from RB group by Rid having count(Rid)>0)
and ReturnDate is null
)
(8)查询电话号码以“139”开头的的读者借阅的书籍
【连接查询语句】
select Bname from Reader
inner join RB on RB.Rid=Reader.Rid
inner join Book on RB.Bid = Book.Bid
where Rnumber like '190%'
【嵌套查询语句】
select Bname from Book
where Bid in (
select Bid from RB inner join Reader on RB.Rid=Reader.Rid
where Rnumber like '190%'
)
附加题(可选):
- 查询书库中除借出的图书外,现存的图书书号、书名、现存数量;
【连接查询语句】
【嵌套查询语句】
select Book.Bid, Book.Bname, Brest_count=(Book.Bcount - RB2.Bcount2)
from Book inner join (
select RBR.Bid, Bcount2 = count(RBR.Bid) from (select Bid from Book
where Bid in (select Bid from RB where ReturnDate is null)) RBR
group by RBR.Bid
) RB2 on RB2.Bid=Book.Bid
(10)查询借了“计算机”类所有书的读者的读者编号及姓名(可选)
【查询语句】
select distinct Reader.Rid, Rname from Reader
inner join RB on Rb.Rid=Reader.Rid
where Bid in (
select Bid from Book
where Btype='计算机'
)
3. 实验问题总结:
实验调试(写出实验过程中遇到的问题,已解决的写出解决方法,可附图)
1.当要用alter向Book表增加count属性时,出现了如图所示的错误;
原因在于Alter只允许添加的列可以为空值,而我设置的是count不可为空。解决方法是在alter table Book add Bcount int not null 语句后加上default 0语句
2.在用 in 做子查询时,出现如下错误:
原因是如果要用in,你后面select必须能只能由一个列组成,如果select后面跟了n个列,自然报那个错误了。
解决方法是将‘*’改为‘Bid’