数据库查询语句练习题一(图书表)

痞子三分冷 提交于 2019-12-07 22:28:37

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
相关题目:

1.显示前3名作者信息
SELECT * FROM authors ORDER BY authors_id LIMIT 0,3;
2.显示倒数后2名作者
SELECT * FROM authors ORDER BY authors_id DESC LIMIT 0,2;
3.显示最新借阅图书的信息
SELECT * FROM books WHERE book_id =
(SELECT book_id FROM borrowinfo ORDER BY borrow_date DESC LIMIT 0,1);
4.显示出版社为“工人出版社”的图书信息
SELECT * FROM books WHERE book_id IN
(SELECT DISTINCT book_id FROM pubinfo WHERE pub_id=
(SELECT pub_id FROM publisher WHERE pub_name='工人出版社'));
5.显示作者姓名中含有“土”的作者出版的图书信息
SELECT * FROM books WHERE book_id IN
(SELECT book_id FROM bookauthor WHERE authors_id IN
(SELECT authors_id FROM authors WHERE authors_name LIKE '%土%'));
6.显示借阅最新出版的图书的读者
SELECT * FROM readers WHERE reader_id IN (
SELECT reader_id FROM borrowinfo WHERE book_id =
(SELECT book_id FROM pubinfo ORDER BY pub_date DESC LIMIT 0,1));
7.显示书名中带有“文”的图书信息
SELECT * FROM books WHERE book_name LIKE '%文%';
8.显示年龄最大的读者借阅图书的信息
SELECT * FROM books WHERE book_id IN(
SELECT book_id FROM borrowinfo WHERE reader_id =
(SELECT reader_id FROM readers ORDER BY reader_age DESC LIMIT 0,1));
1、显示未还书的读者信息
select * from readers where readerid in(select readerid from borrowinfo where backdate is null);
2、显示三个月仍未还书的读者信息
select * from readers where readerid in
(select readerid from borrowinfo where backdate is NULL
and (select date_add(now(),interval -3 month)>borrowdate)
);
3、显示作者姓名中“爽”的位置
select   position('爽' in  authorname) from author where authorname like '%爽%';
select instr(authorname,'爽') from author where authorname like '%爽%';
//position(substr in str):返回子串substr在字符串str中第一次出现的位置
//instr(str,substr):返回子串substr在字符串str中第一次出现的位置
4、显示作者姓名都是由几个字构成的
select char_length(authorname) as '作者名字节数' from author;
5、把书名全部用大写字母显示和用小写字母显示
select upper(bookname), from books ;
select lower(bookname) from books;
6、取出作者简介中的首位空格
select ltrim(authordes) from author;
7、把所有出版社名称中的“出版社”改成“出版社出版’,永久修改
update publisher set pubname=replace(pubname,'出版社','出版社出版');
//replace(str,a,b):返回字符串str中a被b替换后的结果(区分大小写的)
8、把角色中的所有管理员变成‘角色管理员’
update roles set rolerights=replace(rolerights,'管理员','角色管理员');
9、查询图书信息并添加查询时间
select b.*,now()as '查询时间' from books b;
10、把出版日期都更新原日期退后一年多一个月(相当于往后推13个月)
select p.*,date_add(pubdate,interval 13 month) from pubinfo p;
update pubinfo set pubdate=concat_ws('-',year(date_add(pubdate,interval -1 year)),month(date_add(pubdate,interval 1 month)),dayofmonth(pubdate));
11、比当前日期退后一年多一个月
select concat_ws('-',year(date_add(now(),interval -1 year)),month(date_add(now(),interval 1 month)),dayofmonth(now()));
12、查询第一本书和第二本数相差多少个月写完的
select abs(
((select year(pubdate)from pubinfo limit 0,1)-(select year(pubdate)from pubinfo limit 1,1))*12+
(select month(pubdate)from pubinfo limit 0,1)-(select month(pubdate)from pubinfo limit 1,1)
) as 相差时间;
13、把所有的借书日期都显示成(yyyy--mm--dd)(注:[]原样输出)
select concat('[',replace(borrowdate,'-','--'),']') from borrowinfo;
1.显示图书和作者信息,显示字段:图书名称、作者姓名
select b.bookname,authorname from books b,author a,bookauthor bo 
where b.bookid = bo.bookid and bo.authorid = a.authorid;
2.显示出版社出版的图书情况,显示字段:出版社名称、图书名称
select p.pubname,b.bookname from books b,publisher p,pubinfo pu
where pu.bookid = b.bookid and p.pubid=pu.pubid;
3.显示出版社出版的图书情况和作者情况,显示字段:出版社名称、图书名称、作者姓名
select p.pubname,b.bookname,a.authorname from books b,author a,bookauthor bo,publisher p,pubinfo pu
where bo.bookid=b.bookid and bo.authorid=a.authorid and pu.bookid = b.bookid and pu.pubid=p.pubid;
4.显示某位读者借阅某本图书的情况,显示字段:读者姓名、图书名称、借阅时间、还书时间
select r.readername,b.bookname,bo.borrowdate,backdate from books b,readers r,borrowinfo bo
where b.bookid=bo.bookid and r.readerid=bo.readerid;
5.显示某位读者的未还书情况,显示字段:读者姓名、图书名称、借阅时间、还书时间
select r.readername,b.bookname,bo.borrowdate,bo.backdate from books b,readers r,borrowinfo bo
where b.bookid=bo.bookid and r.readerid=bo.readerid and backdate is null ;
6.显示某位读者在某个月的借书情况,显示字段:读者信息、图书名称、借阅时间、还书时间
select readername,bookname,borrowdate,backdate from books b,readers r,borrowinfo bo
where b.bookid=bo.bookid and r.readerid=bo.readerid and year(borrowdate)=2018 and month(borrowdate)=12;
7.显示某本图书的库存情况,显示字段:图书名称、库存量
select bookname,savecount from books b,stocks  s where b.bookid=s.bookid and bookname='郑爽的书';
8.分组统计每个出版社的图书总量,显示字段:出版社名称、图书总量
select p.pubname,count(p.pubid) from publisher p,books b,pubinfo pu
where p.pubid=pu.pubid and b.bookid=pu.bookid
group by p.pubname;
#方法二
select pubname,数量 from publisher p1,
(select p.pubid as pid,count(p.pubid) as 数量 from books b,pubinfo pb,publisher p
where b.bookid=pb.bookid and pb.pubid=p.pubid
group by p.pubid) p2
where p1.pubid=p2.pid;
9.分组统计图书管理系统中不同用户权限的用户数量,显示字段:角色、用户人数
select rolerights as 角色,count(userid) as 用户人数 from roles r,users  u
where u.roleid=r.roleid
group by rolerights
10.计算图书已借阅数量,显示字段:图书名称、已借阅数量
select b.bookname,count(b.bookid) as 借阅数量 from books b,borrowinfo bo
where b.bookid=bo.bookid
group by b.bookname;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!