book表(书本编号,类别,出版社,书名,作者,数量,单价,购买日期,备注)
reader表(读者编号,读者姓名,学院,性别,电话)
borrow表(书本编号,读者编号,借阅日期时间,归还日期时间)
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 = '章小鱼'
)
);
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)
select rname
from reader
where rno not in (
select rno
from borrow
);
select bno, bname
from book
where bno in (
select bno
from borrow
group by bno
having count(rno) > 10
);
select rno,rname
from reader
where rno in (
select rno
from borrow
group by rno
having count(bno) >= 5
);
-- 这题难在如何把两个表的相同属性列相减,
-- 这里用到了“虚表”的方法;
-- 此题借鉴了不少博主的方法,链接放在文末
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;
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;