当前位置:   article > 正文

Mysql 子查询,隐式连接,显式连接,左右外连接 案例_mysql内连接的显式写法与隐式写法

mysql内连接的显式写法与隐式写法

所有查询语句均为手打 ,所以可能我写的有些子查询办法有些蠢,不过我觉得首先能真的用明白子查询语句才是第一步;

每个需求的下的最后一条select语句为该需求最终子查询语句;

这些都能敲出来我觉得子查询第一步是学扎实了;

  1. -- 第一大题:
  2. create database mydb4;
  3. use mydb4;
  4. create table A(
  5. A_ID int primary key auto_increment,
  6. A_NAME varchar(20) not null
  7. );
  8. insert into A values(1,'苹果');
  9. insert into A values(2,'橘子');
  10. insert into A values(3,'香蕉');
  11. create table B(
  12. A_ID int primary key auto_increment,
  13. B_PRICE double
  14. );
  15. insert into B values(1,2.30);
  16. insert into B values(2,3.50);
  17. insert into B values(4,null);
  18. -- 需求:查询出每个水果的价格:
  19. -- 显示内连接:
  20. select I.`A_ID` ,O.`B_PRICE` from a I inner join b O on I.A_ID = O.A_ID;
  21. -- 隐式内连接:
  22. select n.`A_NAME`,o.`B_PRICE` from a n,b o where n.`A_ID` = o.`A_ID`;
  23. -- 外连接查询:
  24. -- 左外连接:
  25. select c.`A_NAME` 水果名,m.`B_PRICE` 价格 from a c left outer join b m on c.`A_ID` = m.`A_ID`
  26. -- 右外连接:
  27. select a.`A_NAME` 水果名, m.`B_PRICE` 价格 from a right outer join b m on a.`A_ID` = m.`A_ID`
  28. -- 子查询:把一个sql的查询结果作为另外一个查询的参数存在
  29. -- 第一步: 在b表中查询最高的价格, 第二步:查询与最高价格相对应的b表id
  30. -- 第三步: 在a表中查询出与b表中刚查询出的id相等的id
  31. select a.`A_NAME` from a where a.`A_ID` =(select b.`A_ID` from b where b.`B_PRICE` = (select MAX(b.`B_PRICE`)from b));
  32. -- -------------------------------------------------------------------------------------
  33. -- 第二大题
  34. create table teacher (
  35. id int(11) not null primary key auto_increment,
  36. name varchar(20) not null unique
  37. );
  38. create table student (
  39. id int(11) not null primary key auto_increment,
  40. name varchar(20) not null unique,
  41. city varchar(40) not null,
  42. age int
  43. ) ;
  44. create table course(
  45. id int(11) not null primary key auto_increment,
  46. name varchar(20) not null unique,
  47. teacher_id int(11) not null,
  48. foreign key (teacher_id) references teacher (id)
  49. );
  50. create table studentcourse (
  51. student_id int not null,
  52. course_id int not null,
  53. score double not null,
  54. foreign key (student_id) references student (id),
  55. foreign key (course_id) references course (id)
  56. );
  57. insert into teacher values(null,'关羽');
  58. insert into teacher values(null,'张飞');
  59. insert into teacher values(null,'赵云');
  60. insert into student values(null,'小王','北京',20);
  61. insert into student values(null,'小李','上海',18);
  62. insert into student values(null,'小周','北京',22);
  63. insert into student values(null,'小刘','北京',21);
  64. insert into student values(null,'小张','上海',22);
  65. insert into student values(null,'小赵','北京',17);
  66. insert into student values(null,'小蒋','上海',23);
  67. insert into student values(null,'小韩','北京',25);
  68. insert into student values(null,'小魏','上海',18);
  69. insert into student values(null,'小明','广州',20);
  70. insert into course values(null,'语文',1);
  71. insert into course values(null,'数学',1);
  72. insert into course values(null,'生物',2);
  73. insert into course values(null,'化学',2);
  74. insert into course values(null,'物理',2);
  75. insert into course values(null,'英语',3);
  76. insert into studentcourse values(1,1,80);
  77. insert into studentcourse values(1,2,90);
  78. insert into studentcourse values(1,3,85);
  79. insert into studentcourse values(1,4,78);
  80. insert into studentcourse values(2,2,53);
  81. insert into studentcourse values(2,3,77);
  82. insert into studentcourse values(2,5,80);
  83. insert into studentcourse values(3,1,71);
  84. insert into studentcourse values(3,2,70);
  85. insert into studentcourse values(3,4,80);
  86. insert into studentcourse values(3,5,65);
  87. insert into studentcourse values(3,6,75);
  88. insert into studentcourse values(4,2,90);
  89. insert into studentcourse values(4,3,80);
  90. insert into studentcourse values(4,4,70);
  91. insert into studentcourse values(4,6,95);
  92. insert into studentcourse values(5,1,60);
  93. insert into studentcourse values(5,2,70);
  94. insert into studentcourse values(5,5,80);
  95. insert into studentcourse values(5,6,69);
  96. insert into studentcourse values(6,1,76);
  97. insert into studentcourse values(6,2,88);
  98. insert into studentcourse values(6,3,87);
  99. insert into studentcourse values(7,4,80);
  100. insert into studentcourse values(8,2,71);
  101. insert into studentcourse values(8,3,58);
  102. insert into studentcourse values(8,5,68);
  103. insert into studentcourse values(9,2,88);
  104. insert into studentcourse values(10,1,77);
  105. insert into studentcourse values(10,2,76);
  106. insert into studentcourse values(10,3,80);
  107. insert into studentcourse values(10,4,85);
  108. insert into studentcourse values(10,5,83);
  109. -- 需求:查询不及格的学生(使用in完成)
  110. select `student_id` from `studentcourse` where `score` < 60;
  111. select * from student where `id` in(select `student_id` from `studentcourse` where `score` < 60);
  112. -- 使用exists 完成:
  113. select * from student where exists (
  114. select `student_id` from `studentcourse`
  115. where `score` < 60 and `studentcourse`.`student_id` = `student`.`id` );
  116. -- 需求:查询获得最高分的学生信息:
  117. select `student_id` from `studentcourse`
  118. where score = (select MAX(score) from `studentcourse`);
  119. select * from `student` where `id` = (select `student_id` from `studentcourse`
  120. where score in (select MAX(score) from `studentcourse`));
  121. -- 需求:查询编号2课程比编号1课程最高成绩高学生信息:
  122. select name from `course` where `id` = 1;
  123. select MAX(score) from `studentcourse` where `course_id` = 1;
  124. select `student_id` from `studentcourse`
  125. where score > (select MAX(score) from `studentcourse` where `course_id` = 1)
  126. and `course_id` = 2;
  127. select * from student where `id` in(
  128. select `student_id` from `studentcourse`
  129. where score > (select MAX(score) from `studentcourse` where `course_id` = 1)
  130. and `course_id` = 2
  131. );
  132. -- 需求:查询编号2课程比编号1课程最高成绩高学生姓名和成绩 (临时表)
  133. select `student`.`name`, ui.`score` from `student` , (
  134. select student_id,score from `studentcourse`
  135. where score > (select MAX(score) from `studentcourse` where `course_id` = 1)
  136. and `course_id` = 2) as ui
  137. where `student`.`id` = ui.`student_id`;
  138. -- 查询平均成绩大于70分的同学的学号和平均成绩
  139. select `student_id`,AVG(`score`)from `studentcourse`
  140. group by `student_id`
  141. having AVG(`score`) >70;
  142. -- 查询所有同学的学号、姓名、选课数、总成绩
  143. select `student_id`, COUNT(`student_id`) 选课数,SUM(`score`) 总成绩 from `studentcourse`
  144. group by `student_id`;
  145. select * from (select `student_id`, COUNT(`student_id`) 选课数,SUM(`score`) 总成绩 from `studentcourse`
  146. group by `student_id`) u ,`student`
  147. where `student`.`id` = u.`student_id`;
  148. select i.id,i.name 姓名 ,i.选课数,i.总成绩 from (select * from (select `student_id`, COUNT(`student_id`) 选课数,SUM(`score`) 总成绩 from `studentcourse`
  149. group by `student_id`) u ,`student`
  150. where `student`.`id` = u.`student_id`) i ;
  151. -- 查询学过赵云老师所教课的同学的学号、姓名
  152. select `id` from `teacher`
  153. where name = "赵云";
  154. select id from `course`
  155. where `teacher_id` = (select `id` from `teacher`where name = "赵云");
  156. select `student_id` from `studentcourse`
  157. where `course_id` = (select id from `course`
  158. where `teacher_id` = (select `id` from `teacher`
  159. where name = "赵云"));
  160. select `id` 学号,`name` 姓名 from `student`
  161. where `id` in (select `student_id` from `studentcourse`
  162. where `course_id` = (select id from `course`
  163. where `teacher_id` = (select `id` from `teacher`
  164. where name = "赵云")));
  165. -- 查询没学过关羽老师课的同学的学号、姓名
  166. select `id` from `teacher`
  167. where name = "关羽";
  168. select id from `course`
  169. where `teacher_id` = (select `id` from `teacher`where name = "关羽");
  170. select `student_id` from `studentcourse`
  171. where `course_id` in (select id from `course`
  172. where `teacher_id` = (select `id` from `teacher`
  173. where name = "关羽"));
  174. select `id`,`name` from `student`where `id` not in (select `student_id` from `studentcourse`
  175. where `course_id` in (select id from `course`
  176. where `teacher_id` = (select `id` from `teacher`
  177. where name = "关羽")));
  178. -- 查询没有学三门课以上的同学的学号、姓名
  179. select COUNT(`student_id`) 选课数,`student_id` id from `studentcourse`
  180. group by `student_id`
  181. having COUNT(`student_id`) <4;
  182. select `student`.`id` 学号 ,`student`.`name` 姓名,选课数 from `student`,(select COUNT(`student_id`) 选课数,`student_id` id from `studentcourse`
  183. group by `student_id`having COUNT(`student_id`) <4) u
  184. where `student`.`id` = u.id;
  185. -- 查询各科成绩最高和最低的分
  186. select `course_id`,MAX(`score`) 最高分,MIN(`score`) 最低分 from `studentcourse`
  187. group by `course_id`;
  188. select i.`name` 学科,最高分,最低分 from (select `course_id`,MAX(`score`) 最高分,MIN(`score`) 最低分 from `studentcourse`
  189. group by `course_id`) p,`course` i
  190. where p.course_id = i.`id`;
  191. -- 查询学生信息和平均成绩
  192. select `student_id` 学号,AVG(`score`) 平均成绩 from `studentcourse`
  193. group by `student_id`;
  194. select 学号,stu.`name`,平均成绩,stu.`age`,stu.`city` from (select `student_id` 学号,AVG(`score`) 平均成绩 from `studentcourse`
  195. group by `student_id`
  196. ) c ,`student` stu
  197. where stu.`id` = 学号
  198. order by 平均成绩 desc;
  199. -- 8、查询上海和北京学生数量
  200. select COUNT(*),`city` from `student`
  201. group by `city`
  202. having `city` = "上海" or `city` = "北京";
  203. -- 9、查询不及格的学生信息和课程信息
  204. select * from `studentcourse`
  205. where `score`<60;
  206. select stu.*,c.`name` 不及格课程号,不及格成绩 from (select `student_id` 学号,`course_id` 不及格课程号,`score` 不及格成绩 from `studentcourse`
  207. where `score`<60) u ,`student` stu ,`course` c
  208. where 学号 = stu.id and 不及格课程号 = c.`id`;
  209. -- 10、统计每门课程的学生选修人数(超过四人的进行统计)
  210. select `course_id` 课程号, COUNT(`course_id`) 选修人数 from `studentcourse`
  211. group by `course_id`
  212. having COUNT(`course_id`)>4;
  213. select `course`.`name` 课程名称,选修人数 from `course` ,(select `course_id` 课程号, COUNT(`course_id`) 选修人数 from `studentcourse`
  214. group by `course_id`
  215. having COUNT(`course_id`)>4) p
  216. where `course`.`id` = 课程号
  217. order by 选修人数 desc;

 _不喜欢数据库

_真香!

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

闽ICP备14008679号