当前位置:   article > 正文

MySQL特别特别基础题_查询书籍的价格在35元以上所有记录 使用trim完成

查询书籍的价格在35元以上所有记录 使用trim完成
  1. -- Table structure for book
  2. create table book (
  3. bno char(20) not null,
  4. bname varchar(50) default null,
  5. author varchar(30) default null,
  6. publish varchar(50) default null,
  7. price float(255,0) default null,
  8. primary key (bno)
  9. )engine = INNODB DEFAULT CHARSET = utf8;
  10. -- Records of book
  11. INSERT INTO `book` VALUES ('111100', '科学出版社历史丛书', '科学', '科学出版社', '108');
  12. INSERT INTO `book` VALUES ('111111', '数据库编程', '张作家', '机械工业出版社', '56');
  13. INSERT INTO `book` VALUES ('222222', '数据库开发', '西红柿', '清华大学出版社', '66');
  14. INSERT INTO `book` VALUES ('333333', '猛兽岛大逃亡', '梦里水乡', '机械工业出版社', '55');
  15. INSERT INTO `book` VALUES ('444444', 'SQL数据库案例', '茶香', '科学出版社', '12');
  16. INSERT INTO `book` VALUES ('555555', '思维导论', 'jison', '机械工业出版社', '65');
  17. INSERT INTO `book` VALUES ('666666', '算法设计', 'jim', '清华大学出版社', '22');
  18. INSERT INTO `book` VALUES ('777777', 'mysql数据库入门', 'kimi', '机械工业出版社', '96');
  19. INSERT INTO `book` VALUES ('888888', '疯狂英语', 'katy', '科学出版社', '33');
  20. INSERT INTO `book` VALUES ('999999', '世界地图', '位居士大夫', '机械工业出版社', '88');
  21. -- Table structure for borrow
  22. DROP TABLE IF EXISTS `borrow`;
  23. CREATE TABLE `borrow` (
  24. `rno` char(8) NOT NULL,
  25. `bno` char(20) NOT NULL,
  26. `bdate` char(8) NOT NULL,
  27. `rdate` char(8) DEFAULT NULL,
  28. KEY `rno` (`rno`),
  29. KEY `bno` (`bno`),
  30. CONSTRAINT `bno` FOREIGN KEY (`bno`) REFERENCES `book` (`bno`),
  31. CONSTRAINT `rno` FOREIGN KEY (`rno`) REFERENCES `reader` (`rno`)
  32. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  33. -- Records of borrow
  34. INSERT INTO `borrow` VALUES ('0001', '111111', '20150403', '20160406');
  35. INSERT INTO `borrow` VALUES ('0001', '333333', '20150206', '20160407');
  36. INSERT INTO `borrow` VALUES ('0002', '222222', '20150207', '20160408');
  37. INSERT INTO `borrow` VALUES ('0002', '555555', '20150208', '20160409');
  38. INSERT INTO `borrow` VALUES ('0003', '444444', '20150209', '20160410');
  39. INSERT INTO `borrow` VALUES ('0008', '444444', '20171012', null);
  40. INSERT INTO `borrow` VALUES ('0009', '999999', '20171215', null);
  41. INSERT INTO `borrow` VALUES ('0002', '222222', '20171116', null);
  42. INSERT INTO `borrow` VALUES ('0003', '666666', '20184545', null);
  43. INSERT INTO `borrow` VALUES ('0003', '888888', '20171141', null);
  44. INSERT INTO `borrow` VALUES ('0002', '888888', '20170678', null);
  45. -- Table structure for reader
  46. DROP TABLE IF EXISTS `reader`;
  47. CREATE TABLE `reader` (
  48. `rno` char(10) NOT NULL,
  49. `rname` char(8) NOT NULL,
  50. `sex` char(2) NOT NULL,
  51. `tel` char(8) DEFAULT NULL,
  52. `department` varchar(30) DEFAULT NULL,
  53. `address` varchar(30) DEFAULT NULL,
  54. PRIMARY KEY (`rno`)
  55. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  56. -- Records of reader
  57. INSERT INTO `reader` VALUES ('0001', '张三', '1', '87818112', '软件', '尚品书院');
  58. INSERT INTO `reader` VALUES ('0002', '李建', '0', '87818283', '网络', null);
  59. INSERT INTO `reader` VALUES ('0003', '王小平', '0', '88320701', '游戏', '尚品书院');
  60. INSERT INTO `reader` VALUES ('0004', '王祝福', '1', '88320701', '游戏', null);
  61. INSERT INTO `reader` VALUES ('0005', '高多多', '1', '87818998', '会计', '华软1号楼');
  62. INSERT INTO `reader` VALUES ('0006', '瑞安', '0', '88320701', '游戏', null);
  63. INSERT INTO `reader` VALUES ('0007', '斯琴', '1', '88320701', '游戏', '绿映楼');
  64. INSERT INTO `reader` VALUES ('0008', '迪迪', '0', '88320701', '游戏', '蓝楹楼');
  65. INSERT INTO `reader` VALUES ('0009', '热吧', '1', '87818282', '工商', '蓝楹楼');
  66. INSERT INTO `reader` VALUES ('0010', '李四四', '1', '8789', '软件', '蓝楹楼');
  67. INSERT INTO `reader` VALUES ('0011', '张四', '1', '8989741', '软件', '尚品书院');
  68. 1、 查询全部图书的图书号、作者、出版社和单价;
  69. select bno,author,publish,price from book;
  70. 2、 查询全体图书的信息,其中单价打8折,并设置该列的别名为“打折价”;
  71. select bno,author,publish,price ,price*0.8 as '打折价'
  72. from book;
  73. 3、 显示所有借过书的借阅者的读者号、并去掉重复行;
  74. select distinct rno
  75. from borrow;
  76. 4、 查询所有单价在20-30元之间的图书信息;
  77. select *
  78. from book
  79. where price between 20 and 30;
  80. 5、 查询所有单价不在20-30元之间的图书信息;
  81. select *
  82. from book
  83. where price not between 20 and 30;
  84. 6、 查询机械工业出版社、科学出版社、人民邮电出版社的图书信息;
  85. select *
  86. from book
  87. where publish in ('机械工业出版社','科学出版社','人民邮电出版社');
  88. 7、 查询既不是机械工业出版社也不是科学出版社出版的图书信息;
  89. select *
  90. from book
  91. where publish not in ('机械工业出版社','科学出版社');
  92. 8、 查询姓名的第二个字符是‘建’并且只有两个字符的读者的 读者号及姓名
  93. select rno,rname
  94. from reader
  95. where rname like '_建';
  96. 9、 查找姓名以‘王’开头的所有读者的读者号及姓名;
  97. select rno,rname
  98. from reader
  99. where rname like '王%';
  100. 10、 查找姓名以王、张、或李开头的所有读者的读者号及姓名;
  101. select rno,rname
  102. from reader
  103. where rname like '王%' or rname like '张%' or rname like '李%';
  104. 11、 查询无归还日期的借阅信息;
  105. select *
  106. from borrow
  107. where rdate is null;
  108. 12、 查询有归还日期的借阅信息;
  109. select *
  110. from borrow
  111. where rdate is not null;
  112. 13、 查询单价在20元以上、30元以下的机械工业出版社出版的图书名及单价;
  113. select bname,price
  114. from book
  115. where price >20 and price < 30 and publish = '机械工业出版社';
  116. 14、 查询机械工业出版社或科学出版社出版的图书名、出版社及单价;
  117. select bname,publish,price
  118. from book
  119. where publish in ('机械工业出版社','科学出版社');
  120. 15、 查询读者的总人数;
  121. select count(*)
  122. from reader;
  123. 16、 查询借阅了图书的读者的总人数;
  124. select count(distinct rno)
  125. from borrow;
  126. 17、 查询机械工业出版社图书的平均价格、最高价、最低价;
  127. select avg(price),max(price),min(price)
  128. from book
  129. where publish = '机械工业出版社';
  130. 18、 查询借阅图书数超过2本的读者号、总本书,并按照借阅本数从大到小排序;
  131. select rno,count(bno) as cn
  132. from borrow
  133. group by rno
  134. having count(bno) >2
  135. order by cn desc;
  136. 19、 查询读者的基本信息及其借阅情况(借阅情况指历史借阅数量,在借数量);
  137. select *
  138. from reader a
  139. left join borrow b on a.rno = b.rno;
  140. 20、 查询读者的读者号、姓名、借阅的图书名、借出日期及归还日期,并按照读者号作升序排序;
  141. select a.rno,rname,bname,bdate,rdate
  142. from reader a
  143. join borrow b on a.rno = b.rno
  144. join book c on b.bno = c.bno
  145. order by rno;
  146. 21、 查询借阅了机械工业出版社,并且书名包含‘数据库’三个字的图书的读者,并显示读者号、姓名、书名、出版社、借出日期及归还日期;
  147. select r.rno as '读者号',r.rname as '姓名',bk.bname,bk.publish,b.rdate,b.bdate from borrow as b
  148. inner join reader as r on r.rno=b.rno
  149. inner join book as bk on bk.bno=b.bno
  150. where bk.publish='机械工业出版社';
  151. 22、 查询至少借阅过1本机械工业出版社的图书的读者的 读者号、姓名、借阅本数,并按借阅本数多少排序;
  152. select r.rno as '学号',r.rname as '姓名',count(1) as '历史借阅量'
  153. from borrow as b
  154. inner join reader as r on r.rno=b.rno
  155. inner join book as bk on bk.bno=b.bno
  156. where bk.publish='机械工业出版社'
  157. group by b.rno having count(1)>=1;
  158. 23、 查询与‘王小平’的办公电话相同的读者的姓名(王小平本人不再列出);
  159. select rname
  160. from reader
  161. where tel = (select tel from reader where rname = '王小平') and rname != '王小平';
  162. 24、 查询办公电话为‘88320701’的所有读者的借阅情况,要求包含借阅了图书的读者和没有借阅的读者,
  163. 显示他们的读者号、姓名、书名及借阅日期;
  164. select reader.rno as ‘学号’,reader.rname as ‘姓名’,book.bname,b.rdate
  165. from borrow as b
  166. RIGHT JOIN reader using(rno)
  167. LEFT JOIN book using(bno);
  168. 25、 查询所有单价小于平均单价的图书的书号、书名及出版社;
  169. select bno,bname,publish from book where price<(select avg(price) from book);
  170. 26、 查询‘科学出版社’的图书的单价比‘机械工业出版社’最高单价还高的图书书名及单价;
  171. select bname,price from book where publish=‘科学出版社’ and
  172. price>(select max(price) from book where publish=‘机械工业出版社’);
  173. 27、 查询已经被借阅过并已经归还的图书信息;
  174. select * from book where bno in (select bno from borrow where bdate is not null and rdate is not null)
  175. 28、 查询从未被借阅过的图书信息;
  176. select * from book where bno not in (select bno from borrow )
  177. 29、 查询正在被借阅的图书信息;
  178. select * from book where bno in (select bno from borrow where rdate is null)
  179. 30、 查询软件系借了书还没有还的读者学号姓名。
  180. select DISTINCT r.rno as ‘学号’,r.rname as ‘姓名’
  181. from borrow as b
  182. inner join reader as r on r.rno=b.rno
  183. inner join book as bk on bk.bno=b.bno
  184. where b.rdate is null;
  185. 31、 查询借阅图书总数最多的宿舍楼
  186. select reader.address from reader
  187. left join borrow using(rno)
  188. GROUP BY reader.address ORDER BY count(borrow.bno) desc LIMIT 1;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/545984
推荐阅读
相关标签
  

闽ICP备14008679号