mysql常使用语句学习

您所在的位置:网站首页 出88会员号 mysql常使用语句学习

mysql常使用语句学习

2024-07-17 14:37| 来源: 网络整理| 查看: 265

mysql常使用语句 1、 查询全部图书的图书号、书名、作者、出版社和单价;2、 查询全部图书的信息,其中单价打8折,并设置该列的别名为“打折价”;3、 显示所有借阅表中的读者号、并去掉重复行;4、 查询所有单价在20-30元之间的图书信息;5、 查询所有单价不在20-30元之间的图书信息;6、 查询机械工业出版社、科学出版社、人民邮电出版社的图书信息;7、 查询既不是机械工业出版社也不是科学出版社出版的图书信息;8、 查询姓名的第二个字符是‘四’并且只有两个字符的读者的读者号及姓名;9、 查找姓名以‘王’开头的所有读者的读者号及姓名;10、 查找姓名以王、张、或李开头的所有读者的读者号及姓名;11、 查询无归还日期的借阅信息;12、 查询有归还日期的借阅信息;13、 查询单价在20元以上、30元以下的机械工业出版社出版的图书名及单价;14、 查询机械工业出版社或科学出版社出版的图书名、出版社及单价;15、 查询读者的总人数;16、 查询借阅了图书的读者的总人数;17、 查询机械工业出版社图书的平均价格、最高价、最低价;18、 查询借阅图书数超过2本的读者号、总本数,并按照借阅本数从大到小排序;19、 查询读者的读者号、读者姓名及其借阅书号、借出日期;20、 查询读者的读者号、姓名、借阅的图书名、借出日期及归还日期;21、 查询借阅了机械工业出版社,并且书名包含‘数据库’三个字的图书的读者,并显示读者号、姓名、书名、出版社、借出日期及归还日期;22、 查询至少借阅过1本机械工业出版社的图书的读者的读者号、姓名、书名及借阅本书,并按照借阅本数多少排序;23、 查询与‘王小平’的办公电话相同的读者的姓名;24、 查询办公电话为‘88320701’的所有读者的借阅情况,要求包含借阅了图书的读者和没有借阅的读者,显示他们的读者号、姓名、书名及借阅日期;25、 查询所有单价小于平均单价的图书的书号、书名及出版社;26、 查询‘科学出版社’的图书中单价比‘机械工业出版社’最高单价还高的图书书名及单价;27、 查询‘科学出版社’的图书中单价比‘机械工业出版社’最低单价高的图书书名及单价;28、 查询已经被借阅过并已经归还的图书信息;29、 查询从未被借阅过的图书书号、书名、作者和出版社;30、 查询正在被借阅的图书书号、书名和出版社;32、查询节约图书总数最多的宿舍楼

一、作业 在这里插入图片描述

1、 查询全部图书的图书号、书名、作者、出版社和单价;

use library; select * from book; 或 select bno,bname,author,publish,price from book;

2、 查询全部图书的信息,其中单价打8折,并设置该列的别名为“打折价”;

select bno,bname,author,publish,price * 0.8 as “打折价” from book;

3、 显示所有借阅表中的读者号、并去掉重复行;

select distinct rno from borrow;

4、 查询所有单价在20-30元之间的图书信息;

select * from book where price between 20 and 30; //连续值

5、 查询所有单价不在20-30元之间的图书信息;

select * from book where price not between 20 and 30;

6、 查询机械工业出版社、科学出版社、人民邮电出版社的图书信息;

select * from book where publish in (‘机械工业出版社’,‘科学出版社’,‘人民邮电出版社’); //离散值

7、 查询既不是机械工业出版社也不是科学出版社出版的图书信息;

select * from book where publish not in (‘机械工业出版社’,‘科学出版社’);

8、 查询姓名的第二个字符是‘四’并且只有两个字符的读者的读者号及姓名;

select rno, rname from reader where rname like ‘_四’

9、 查找姓名以‘王’开头的所有读者的读者号及姓名;

select rno, rname from reader where rname like ‘王%’;

10、 查找姓名以王、张、或李开头的所有读者的读者号及姓名;

select rno, rname from reader where rname like ‘王%’ or rname like ‘张%’ or rname like ‘李%’;

11、 查询无归还日期的借阅信息;

select * from borrow where rdate is null;

12、 查询有归还日期的借阅信息;

select * from borrow where rdate is not null;

13、 查询单价在20元以上、30元以下的机械工业出版社出版的图书名及单价;

select bname, price from book where publish = ‘机械工业出版社’ and price between 20 and 30;

14、 查询机械工业出版社或科学出版社出版的图书名、出版社及单价;

select bname, publish, price from book where publish in (‘机械工业出版社’, ‘科学出版社’);

15、 查询读者的总人数;

select count(*) from reader;

16、 查询借阅了图书的读者的总人数;

select count(distinct borrow.rno) from reader, borrow where reader.rno = borrow.rno; 可以再精简一下命令吗?

17、 查询机械工业出版社图书的平均价格、最高价、最低价;

select avg(price) as ‘平均价格’, max(price) as ‘最高价’, min(price) as ‘最低价’ from book where publish = ‘机械工业出版社’;

18、 查询借阅图书数超过2本的读者号、总本数,并按照借阅本数从大到小排序;

select rno, count(bno) from borrow group by rno having count(*) > 2 order by count(bno) desc;

19、 查询读者的读者号、读者姓名及其借阅书号、借出日期;

select reader.rno, rname, borrow.bno, bdate from borrow, reader where borrow.rno = reader.rno ;或者select reader.rno, rname, borrow.bno, bdate from borrow join reader using(rno);

20、 查询读者的读者号、姓名、借阅的图书名、借出日期及归还日期;

select reader.rno, rname, bname, bdate, rdate from book, borrow, reader where borrow.rno = reader.rno and book.bno = borrow.bno; select reader.rno, rname, bname, bdate, rdate from borrow left join reader using(rno) left join book using(bno);

21、 查询借阅了机械工业出版社,并且书名包含‘数据库’三个字的图书的读者,并显示读者号、姓名、书名、出版社、借出日期及归还日期;

select borrow.rno, rname, bname, publish, bdate, rdate from book, borrow, reader where publish = ‘机械工业出版社’ and bname like ‘%数据库%’ and borrow.rno = reader.rno and book.bno = borrow.bno;

22、 查询至少借阅过1本机械工业出版社的图书的读者的读者号、姓名、书名及借阅本书,并按照借阅本数多少排序;

select borrow.rno, rname, bname, count(borrow.bno) from book, borrow, reader where borrow.rno = reader.rno and book.bno = borrow.bno and publish = ‘机械工业出版社’ group by rno having count(borrow.bno) >= 1 order by count(borrow.bno) desc;

23、 查询与‘王小平’的办公电话相同的读者的姓名;

select rname from reader where tel in (select tel from reader where rname = ‘王小平’);

24、 查询办公电话为‘88320701’的所有读者的借阅情况,要求包含借阅了图书的读者和没有借阅的读者,显示他们的读者号、姓名、书名及借阅日期;

select borrow.rno, rname, bname, bdate from reader left join borrow on borrow.rno = reader.rno join book on book.bno = borrow.bno where tel = ‘88320701’;

25、 查询所有单价小于平均单价的图书的书号、书名及出版社;

select bno, bname, publish from book where price < (select avg(price) from book);

26、 查询‘科学出版社’的图书中单价比‘机械工业出版社’最高单价还高的图书书名及单价;

select bname, price from book where publish = ‘科学出版社’ and price > ( select max(price) from book where publish = ‘机械工业出版社’);

27、 查询‘科学出版社’的图书中单价比‘机械工业出版社’最低单价高的图书书名及单价;

select bname, price from book where publish = ‘科学出版社’ and price > ( select min(price) from book where publish = ‘机械工业出版社’);

28、 查询已经被借阅过并已经归还的图书信息;

select * from book where bno in (select bno from borrow where rdate is not null);

29、 查询从未被借阅过的图书书号、书名、作者和出版社;

select bno, bname, author, publish from book where bno not in (select bno from borrow);

30、 查询正在被借阅的图书书号、书名和出版社;

select bno, bname, publish from book where bno in (select bno from borrow where bdate is not null and rdate is null);

32、查询节约图书总数最多的宿舍楼

select address ,count(address) from reader,borrow where reader.rno=borrow.rno group by address order by count(address) desc limit 1;



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3