表名和字段

  1. –.学生表
  2. Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
  3. –.课程表
  4. Course(c_id,c_name,t_id) –课程编号, 课程名称, 教师编号
  5. –.教师表
  6. Teacher(t_id,t_name) –教师编号,教师姓名
  7. –.成绩表
  8. Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据

  1. --建表
  2. --学生表
  3. CREATE TABLE `Student`(
  4. `s_id` VARCHAR(20),
  5. `s_name` VARCHAR(20) NOT NULL DEFAULT '',
  6. `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
  7. `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
  8. PRIMARY KEY(`s_id`)
  9. );
  10. --课程表
  11. CREATE TABLE `Course`(
  12. `c_id` VARCHAR(20),
  13. `c_name` VARCHAR(20) NOT NULL DEFAULT '',
  14. `t_id` VARCHAR(20) NOT NULL,
  15. PRIMARY KEY(`c_id`)
  16. );
  17. --教师表
  18. CREATE TABLE `Teacher`(
  19. `t_id` VARCHAR(20),
  20. `t_name` VARCHAR(20) NOT NULL DEFAULT '',
  21. PRIMARY KEY(`t_id`)
  22. );
  23. --成绩表
  24. CREATE TABLE `Score`(
  25. `s_id` VARCHAR(20),
  26. `c_id` VARCHAR(20),
  27. `s_score` INT(3),
  28. PRIMARY KEY(`s_id`,`c_id`)
  29. );
  30. --插入学生表测试数据
  31. insert into Student values('' , '赵雷' , '1990-01-01' , '男');
  32. insert into Student values('' , '钱电' , '1990-12-21' , '男');
  33. insert into Student values('' , '孙风' , '1990-05-20' , '男');
  34. insert into Student values('' , '李云' , '1990-08-06' , '男');
  35. insert into Student values('' , '周梅' , '1991-12-01' , '女');
  36. insert into Student values('' , '吴兰' , '1992-03-01' , '女');
  37. insert into Student values('' , '郑竹' , '1989-07-01' , '女');
  38. insert into Student values('' , '王菊' , '1990-01-20' , '女');
  39. --课程表测试数据
  40. insert into Course values('' , '语文' , '');
  41. insert into Course values('' , '数学' , '');
  42. insert into Course values('' , '英语' , '');
  43.  
  44. --教师表测试数据
  45. insert into Teacher values('' , '张三');
  46. insert into Teacher values('' , '李四');
  47. insert into Teacher values('' , '王五');
  48.  
  49. --成绩表测试数据
  50. insert into Score values('' , '' , 80);
  51. insert into Score values('' , '' , 90);
  52. insert into Score values('' , '' , 99);
  53. insert into Score values('' , '' , 70);
  54. insert into Score values('' , '' , 60);
  55. insert into Score values('' , '' , 80);
  56. insert into Score values('' , '' , 80);
  57. insert into Score values('' , '' , 80);
  58. insert into Score values('' , '' , 80);
  59. insert into Score values('' , '' , 50);
  60. insert into Score values('' , '' , 30);
  61. insert into Score values('' , '' , 20);
  62. insert into Score values('' , '' , 76);
  63. insert into Score values('' , '' , 87);
  64. insert into Score values('' , '' , 31);
  65. insert into Score values('' , '' , 34);
  66. insert into Score values('' , '' , 89);
  67. insert into Score values('' , '' , 98);

表数据如下:

  1. -- 准备条件,去掉 sql_mode ONLY_FULL_GROUP_BY 否则此种情况下会报错:
  2. -- Expression #1 of select list is not in group by clause and contains nonaggregated column 'userinfo.
  3. -- 原因:
  4. -- MySQL 5.7.5up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),
  5. -- 那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。
  6. -- (在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅MySQL 5.6参考手册。)
  7. -- 执行以下个命令,可以查看 sql_mode 的内容。
  8. SHOW SESSION VARIABLES;
  9. SHOW GLOBAL VARIABLES;
  10. select @@sql_mode;
  11. -- 更改
  12. set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  13. set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

练习题和sql语句

  1. -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
  2. select st.*,sc.s_score as '语文' ,sc2.s_score '数学'
  3. from student st
  4. left join score sc on sc.s_id=st.s_id and sc.c_id=''
  5. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id=''
  6. where sc.s_score>sc2.s_score
  7.  
  8. -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
  9. select st.*,sc.s_score '语文',sc2.s_score '数学' from student st
  10. left join score sc on sc.s_id=st.s_id and sc.c_id=''
  11. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id=''
  12. where sc.s_score<sc2.s_score
  13.  
  14. -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
  15. select st.s_id,st.s_name,ROUND(AVG(sc.s_score),2) "平均成绩" from student st
  16. left join score sc on sc.s_id=st.s_id
  17. group by st.s_id having AVG(sc.s_score)>=60
  18.  
  19. -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
  20. -- (包括有成绩的和无成绩的)
  21. select st.s_id,st.s_name,(case when ROUND(AVG(sc.s_score),2) is null then 0 else ROUND(AVG(sc.s_score),2) end ) "平均成绩" from student st
  22. left join score sc on sc.s_id=st.s_id
  23. group by st.s_id having AVG(sc.s_score)<60 or AVG(sc.s_score) is NULL
  24.  
  25. -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
  26.  
  27. select st.s_id,st.s_name,count(sc.c_id) "选课总数",sum(case when sc.s_score is null then 0 else sc.s_score end) "总成绩"
  28. from student st
  29. left join score sc on st.s_id = sc.s_id
  30. group by st.s_id
  31.  
  32. -- 6、查询"李"姓老师的数量
  33. select t.t_name,count(t.t_id) from teacher t
  34. group by t.t_id having t.t_name like "李%";
  35.  
  36. -- 7、查询学过"张三"老师授课的同学的信息
  37. select st.* from student st
  38. left join score sc on sc.s_id=st.s_id
  39. left join course c on c.c_id=sc.c_id
  40. left join teacher t on t.t_id=c.t_id
  41. where t.t_name="张三"
  42.  
  43. -- 8、查询没学过"张三"老师授课的同学的信息
  44. -- 张三老师教的课
  45. select c.* from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三"
  46. -- 有张三老师课成绩的st.s_id
  47. select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")
  48. -- 不在上面查到的st.s_id的学生信息,即没学过张三老师授课的同学信息
  49. select st.* from student st where st.s_id not in(
  50. select sc.s_id from score sc where sc.c_id in (select c.c_id from course c left join teacher t on t.t_id=c.t_id where t.t_name="张三")
  51. )
  52.  
  53. -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
  54. select st.* from student st
  55. inner join score sc on sc.s_id = st.s_id
  56. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  57. where st.s_id in (
  58. select st2.s_id from student st2
  59. inner join score sc2 on sc2.s_id = st2.s_id
  60. inner join course c2 on c2.c_id=sc2.c_id and c2.c_id="02"
  61. )
  62.  
  63. select a.*
  64. from
  65. student a,
  66. score b,
  67. score c
  68. where
  69. a.s_id = b.s_id
  70. and a.s_id = c.s_id
  71. and b.c_id = ''
  72. and c.c_id = '';
  73.  
  74. -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
  75. select st.* from student st
  76. inner join score sc on sc.s_id = st.s_id
  77. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  78. where st.s_id not in (
  79. select st2.s_id from student st2
  80. inner join score sc2 on sc2.s_id = st2.s_id
  81. inner join course c2 on c2.c_id=sc2.c_id and c2.c_id="02"
  82. )
  83.  
  84. -- 11、查询没有学全所有课程的同学的信息
  85. select * from student where s_id not in (
  86. select st.s_id from student st
  87. inner join score sc on sc.s_id = st.s_id and sc.c_id="01"
  88. where st.s_id in (
  89. select st1.s_id from student st1
  90. inner join score sc2 on sc2.s_id = st1.s_id and sc2.c_id="02"
  91. ) and st.s_id in (
  92. select st2.s_id from student st2
  93. inner join score sc2 on sc2.s_id = st2.s_id and sc2.c_id="03"
  94. ))
  95.  
  96. select a.*
  97. from student a
  98. left join score b on a.s_id = b.s_id
  99. group by
  100. a.s_id
  101. having
  102. count(b.c_id) != '';
  103.  
  104. -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
  105. select distinct st.* from student st
  106. left join score sc on sc.s_id=st.s_id
  107. where sc.c_id in (
  108. select sc2.c_id from student st2
  109. left join score sc2 on sc2.s_id=st2.s_id
  110. where st2.s_id =''
  111. )
  112.  
  113. -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
  114. select st.* from student st
  115. left join score sc on sc.s_id=st.s_id
  116. group by st.s_id
  117. having group_concat(sc.c_id) =
  118. (
  119. select group_concat(sc2.c_id) from student st2
  120. left join score sc2 on sc2.s_id=st2.s_id
  121. where st2.s_id =''
  122. )
  123.  
  124. -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
  125. select st.s_name from student st
  126. where st.s_id not in (
  127. select sc.s_id from score sc
  128. inner join course c on c.c_id=sc.c_id
  129. inner join teacher t on t.t_id=c.t_id and t.t_name="张三"
  130. )
  131.  
  132. -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
  133. select st.s_id,st.s_name,avg(sc.s_score) from student st
  134. left join score sc on sc.s_id=st.s_id
  135. where sc.s_id in (
  136. select sc.s_id from score sc
  137. where sc.s_score<60 or sc.s_score is NULL
  138. group by sc.s_id having COUNT(1)>=2
  139. )
  140. group by st.s_id
  141.  
  142. -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
  143. select st.*,sc.s_score from student st
  144. inner join score sc on sc.s_id=st.s_id and sc.c_id="01" and sc.s_score<60
  145. order by sc.s_score desc
  146.  
  147. select st.*,sc.s_score from student st
  148. left join score sc on sc.s_id=st.s_id
  149. where sc.c_id="01" and sc.s_score<60
  150. order by sc.s_score desc
  151.  
  152. -- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
  153. select st.s_id,st.s_name,avg(sc4.s_score) "平均分",sc.s_score "语文",sc2.s_score "数学",sc3.s_score "英语" from student st
  154. left join score sc on sc.s_id=st.s_id and sc.c_id="01"
  155. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id="02"
  156. left join score sc3 on sc3.s_id=st.s_id and sc3.c_id="03"
  157. left join score sc4 on sc4.s_id=st.s_id
  158. group by st.s_id
  159. order by avg(sc4.s_score) desc
  160.  
  161. select st.s_id,st.s_name,
  162. (case when avg(sc4.s_score) is null then 0 else avg(sc4.s_score) end) "平均分",
  163. (case when sc.s_score is null then 0 else sc.s_score end) "语文",
  164. (case when sc2.s_score is null then 0 else sc2.s_score end) "数学",
  165. (case when sc3.s_score is null then 0 else sc3.s_score end) "英语"
  166. from student st
  167. left join score sc on sc.s_id=st.s_id and sc.c_id="01"
  168. left join score sc2 on sc2.s_id=st.s_id and sc2.c_id="02"
  169. left join score sc3 on sc3.s_id=st.s_id and sc3.c_id="03"
  170. left join score sc4 on sc4.s_id=st.s_id
  171. group by st.s_id
  172. order by avg(sc4.s_score) desc
  173.  
  174. -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
  175. -- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
  176. select c.c_id,c.c_name,max(sc.s_score) "最高分",MIN(sc2.s_score) "最低分",avg(sc3.s_score) "平均分"
  177. ,((select count(s_id) from score where s_score>=60 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "及格率"
  178. ,((select count(s_id) from score where s_score>=70 and s_score<80 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "中等率"
  179. ,((select count(s_id) from score where s_score>=80 and s_score<90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优良率"
  180. ,((select count(s_id) from score where s_score>=90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优秀率"
  181. from course c
  182. left join score sc on sc.c_id=c.c_id
  183. left join score sc2 on sc2.c_id=c.c_id
  184. left join score sc3 on sc3.c_id=c.c_id
  185. group by c.c_id
  186.  
  187. -- 19、按各科成绩进行排序,并显示排名(实现不完全)
  188. -- mysql没有rank函数
  189. -- @score是为了防止用union all 后打乱了顺序
  190. select c1.s_id,c1.c_id,c1.c_name,@score:=c1.s_score,@i:=@i+1 from (select c.c_name,sc.* from course c
  191. left join score sc on sc.c_id=c.c_id
  192. where c.c_id="01" order by sc.s_score desc) c1 ,
  193. (select @i:=0) a
  194. union all
  195. select c2.s_id,c2.c_id,c2.c_name,c2.s_score,@ii:=@ii+1 from (select c.c_name,sc.* from course c
  196. left join score sc on sc.c_id=c.c_id
  197. where c.c_id="02" order by sc.s_score desc) c2 ,
  198. (select @ii:=0) aa
  199. union all
  200. select c3.s_id,c3.c_id,c3.c_name,c3.s_score,@iii:=@iii+1 from (select c.c_name,sc.* from course c
  201. left join score sc on sc.c_id=c.c_id
  202. where c.c_id="03" order by sc.s_score desc) c3;
  203. set @iii=0;
  204.  
  205. -- 20、查询学生的总成绩并进行排名
  206. select st.s_id,st.s_name
  207. ,(case when sum(sc.s_score) is null then 0 else sum(sc.s_score) end)
  208. from student st
  209. left join score sc on sc.s_id=st.s_id
  210. group by st.s_id order by sum(sc.s_score) desc
  211.  
  212. -- 21、查询不同老师所教不同课程平均分从高到低显示
  213. select t.t_id,t.t_name,c.c_name,avg(sc.s_score) from teacher t
  214. left join course c on c.t_id=t.t_id
  215. left join score sc on sc.c_id =c.c_id
  216. group by t.t_id
  217. order by avg(sc.s_score) desc
  218.  
  219. -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
  220. select a.* from (
  221. select st.*,c.c_id,c.c_name,sc.s_score from student st
  222. left join score sc on sc.s_id=st.s_id
  223. inner join course c on c.c_id =sc.c_id and c.c_id="01"
  224. order by sc.s_score desc LIMIT 1,2 ) a
  225. union all
  226. select b.* from (
  227. select st.*,c.c_id,c.c_name,sc.s_score from student st
  228. left join score sc on sc.s_id=st.s_id
  229. inner join course c on c.c_id =sc.c_id and c.c_id="02"
  230. order by sc.s_score desc LIMIT 1,2) b
  231. union all
  232. select c.* from (
  233. select st.*,c.c_id,c.c_name,sc.s_score from student st
  234. left join score sc on sc.s_id=st.s_id
  235. inner join course c on c.c_id =sc.c_id and c.c_id="03"
  236. order by sc.s_score desc LIMIT 1,2) c
  237.  
  238. -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
  239. select c.c_id,c.c_name
  240. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=100 and sc.s_score>80)/(select count(1) from score sc where sc.c_id=c.c_id )) "100-85"
  241. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=85 and sc.s_score>70)/(select count(1) from score sc where sc.c_id=c.c_id )) "85-70"
  242. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=70 and sc.s_score>60)/(select count(1) from score sc where sc.c_id=c.c_id )) "70-60"
  243. ,((select count(1) from score sc where sc.c_id=c.c_id and sc.s_score<=60 and sc.s_score>=0)/(select count(1) from score sc where sc.c_id=c.c_id )) "60-0"
  244. from course c order by c.c_id
  245.  
  246. -- 24、查询学生平均成绩及其名次
  247. set @i=0;
  248. select a.*,@i:=@i+1 from (
  249. select st.s_id,st.s_name,round((case when avg(sc.s_score) is null then 0 else avg(sc.s_score) end),2) "平均分" from student st
  250. left join score sc on sc.s_id=st.s_id
  251. group by st.s_id order by sc.s_score desc) a
  252.  
  253. -- 25、查询各科成绩前三名的记录
  254. select a.* from (
  255. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  256. left join score sc on sc.s_id=st.s_id
  257. inner join course c on c.c_id=sc.c_id and c.c_id=''
  258. order by sc.s_score desc LIMIT 0,3) a
  259. union all
  260. select b.* from (
  261. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  262. left join score sc on sc.s_id=st.s_id
  263. inner join course c on c.c_id=sc.c_id and c.c_id=''
  264. order by sc.s_score desc LIMIT 0,3) b
  265. union all
  266. select c.* from (
  267. select st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score from student st
  268. left join score sc on sc.s_id=st.s_id
  269. inner join course c on c.c_id=sc.c_id and c.c_id=''
  270. order by sc.s_score desc LIMIT 0,3) c
  271.  
  272. -- 26、查询每门课程被选修的学生数
  273. select c.c_id,c.c_name,count(1) from course c
  274. left join score sc on sc.c_id=c.c_id
  275. inner join student st on st.s_id=c.c_id
  276. group by st.s_id
  277.  
  278. -- 27、查询出只有两门课程的全部学生的学号和姓名
  279. select st.s_id,st.s_name from student st
  280. left join score sc on sc.s_id=st.s_id
  281. inner join course c on c.c_id=sc.c_id
  282. group by st.s_id having count(1)=2
  283.  
  284. -- 28、查询男生、女生人数
  285. select st.s_sex,count(1) from student st group by st.s_sex
  286.  
  287. -- 29、查询名字中含有"风"字的学生信息
  288. select st.* from student st where st.s_name like "%风%";
  289.  
  290. -- 30、查询同名同性学生名单,并统计同名人数
  291. select st.*,count(1) from student st group by st.s_name,st.s_sex having count(1)>1
  292.  
  293. -- 31、查询1990年出生的学生名单
  294. select st.* from student st where st.s_birth like "1990%";
  295.  
  296. -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
  297. select c.c_id,c.c_name,avg(sc.s_score) from course c
  298. inner join score sc on sc.c_id=c.c_id
  299. group by c.c_id order by avg(sc.s_score) desc,c.c_id asc
  300.  
  301. -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
  302. select st.s_id,st.s_name,avg(sc.s_score) from student st
  303. left join score sc on sc.s_id=st.s_id
  304. group by st.s_id having avg(sc.s_score)>=85
  305.  
  306. -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
  307. select st.s_id,st.s_name,sc.s_score from student st
  308. inner join score sc on sc.s_id=st.s_id and sc.s_score<60
  309. inner join course c on c.c_id=sc.c_id and c.c_name ="数学"
  310.  
  311. -- 35、查询所有学生的课程及分数情况;
  312. select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  313. left join score sc on sc.s_id=st.s_id
  314. left join course c on c.c_id =sc.c_id
  315. order by st.s_id,c.c_name
  316.  
  317. -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
  318. select st2.s_id,st2.s_name,c2.c_name,sc2.s_score from student st2
  319. left join score sc2 on sc2.s_id=st2.s_id
  320. left join course c2 on c2.c_id=sc2.c_id
  321. where st2.s_id in(
  322. select st.s_id from student st
  323. left join score sc on sc.s_id=st.s_id
  324. group by st.s_id having min(sc.s_score)>=70)
  325. order by s_id
  326.  
  327. -- 37、查询不及格的课程
  328. select st.s_id,c.c_name,st.s_name,sc.s_score from student st
  329. inner join score sc on sc.s_id=st.s_id and sc.s_score<60
  330. inner join course c on c.c_id=sc.c_id
  331.  
  332. -- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
  333. select st.s_id,st.s_name,sc.s_score from student st
  334. inner join score sc on sc.s_id=st.s_id and sc.c_id="01" and sc.s_score>=80
  335.  
  336. -- 39、求每门课程的学生人数
  337. select c.c_id,c.c_name,count(1) from course c
  338. inner join score sc on sc.c_id=c.c_id
  339. group by c.c_id
  340.  
  341. -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
  342. select st.*,c.c_name,sc.s_score,t.t_name from student st
  343. inner join score sc on sc.s_id=st.s_id
  344. inner join course c on c.c_id=sc.c_id
  345. inner join teacher t on t.t_id=c.t_id and t.t_name="张三"
  346. order by sc.s_score desc
  347. limit 0,1
  348.  
  349. -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
  350. select st.s_id,st.s_name,sc.c_id,sc.s_score from student st
  351. left join score sc on sc.s_id=st.s_id
  352. left join course c on c.c_id=sc.c_id
  353. where (
  354. select count(1) from student st2
  355. left join score sc2 on sc2.s_id=st2.s_id
  356. left join course c2 on c2.c_id=sc2.c_id
  357. where sc.s_score=sc2.s_score and c.c_id!=c2.c_id
  358. )>1
  359.  
  360. -- 42、查询每门功成绩最好的前两名
  361. select a.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  362. left join score sc on sc.s_id=st.s_id
  363. inner join course c on c.c_id=sc.c_id and c.c_id="01"
  364. order by sc.s_score desc limit 0,2) a
  365. union all
  366. select b.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  367. left join score sc on sc.s_id=st.s_id
  368. inner join course c on c.c_id=sc.c_id and c.c_id="02"
  369. order by sc.s_score desc limit 0,2) b
  370. union all
  371. select c.* from (select st.s_id,st.s_name,c.c_name,sc.s_score from student st
  372. left join score sc on sc.s_id=st.s_id
  373. inner join course c on c.c_id=sc.c_id and c.c_id="03"
  374. order by sc.s_score desc limit 0,2) c
  375.  
  376. -- 借鉴(更准确,漂亮):
  377. select a.s_id,a.c_id,a.s_score from score a
  378. where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 order by a.c_id
  379.  
  380. -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
  381. -- 若人数相同,按课程号升序排列
  382. select sc.c_id,count(1) from score sc
  383. left join course c on c.c_id=sc.c_id
  384. group by c.c_id having count(1)>5
  385. order by count(1) desc,sc.c_id asc
  386.  
  387. -- 44、检索至少选修两门课程的学生学号
  388. select st.s_id from student st
  389. left join score sc on sc.s_id=st.s_id
  390. group by st.s_id having count(1)>=2
  391.  
  392. -- 45、查询选修了全部课程的学生信息
  393. select st.* from student st
  394. left join score sc on sc.s_id=st.s_id
  395. group by st.s_id having count(1)=(select count(1) from course)
  396.  
  397. -- 46、查询各学生的年龄
  398. select st.*,timestampdiff(year,st.s_birth,now()) from student st
  399.  
  400. -- 47、查询本周过生日的学生
  401. -- 此处可能有问题,week函数取的为当前年的第几周,2017-12-12是第50周而2018-12-12是第49周,可以取月份,day,星期几(%w),
  402. -- 再判断本周是否会持续到下一个月进行判断,太麻烦,不会写
  403. select st.* from student st
  404. where week(now())=week(date_format(st.s_birth,'%Y%m%d'))
  405.  
  406. -- 48、查询下周过生日的学生
  407. select st.* from student st
  408. where week(now())+1=week(date_format(st.s_birth,'%Y%m%d'))
  409.  
  410. -- 49、查询本月过生日的学生
  411. select st.* from student st
  412. where month(now())=month(date_format(st.s_birth,'%Y%m%d'))
  413.  
  414. -- 50、查询下月过生日的学生
  415. -- 注意:当 当前月为12时,用month(now())+113而不是1,可用timestampadd()函数或mod取模
  416. select st.* from student st
  417. where month(timestampadd(month,1,now()))=month(date_format(st.s_birth,'%Y%m%d'))
  418. --
  419. select st.* from student st where (month(now()) + 1) mod 12 = month(date_format(st.s_birth,'%Y%m%d'))

sql语句练习50题(Mysql版) 围观的更多相关文章

  1. sql语句练习50题(Mysql版-详加注释)

    表名和字段 1.学生表       Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表       Course(c_id, ...

  2. sql语句练习50题(Mysql版)

    表名和字段–1.学生表Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别–2.课程表Course(c_id,c_name,t_id) – ...

  3. -sql语句练习50题(Mysql学习练习版)

    –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课 ...

  4. MySQL经典练习题及答案,常用SQL语句练习50题

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...

  5. 经典SQL语句基础50题

    很全面的sql语句大全.都是很基础性的,今天特意整理了下.大家互相学习.大家有好的都可以分享出来,  分享也是一种快乐. --创建数据库 create database SQL50 --打开SQL50 ...

  6. [转载]sql语句练习50题

    Student(Sid,Sname,Sage,Ssex) 学生表 Course(Cid,Cname,Tid) 课程表 SC(Sid,Cid,score) 成绩表 Teacher(Tid,Tname) ...

  7. sql语句练习50题

    Student(Sid,Sname,Sage,Ssex) 学生表 Course(Cid,Cname,Tid) 课程表 SC(Sid,Cid,score) 成绩表 Teacher(Tid,Tname) ...

  8. PHP如何通过SQL语句将数据写入MySQL数据库呢?

    1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...

  9. 解决乱码的方法是,在执行SQL语句之前,将MySQL以下三个系统参数设置为与服务器字符集character-set-server相同的字符集

    character-set-server/default-character-set:服务器字符集,默认情况下所采用的. character-set-database:数据库字符集. characte ...

随机推荐

  1. 常见框架和WSGI协议

    三大框架对比 Django 大而全 自带的功能特别特别多 类似于航空母舰 有时候过于笨重 Flask 小而精,只保留了核心功能,其他可以自由选择 第三方的模块特别特别多,如果将flask第三方模块全部 ...

  2. WebStrom安装Markdown插件

    安装步骤 File→Settings→Plugins→关键字搜索markdown→选择Markdown Navigator→点击Install→出现下载弹窗,等待下载完毕→重启Webstrom 效果预 ...

  3. 「福利」Java Swing 编写的可视化算法工程,包含树、图和排序

    之前在整理<学习排序算法,结合这个方法太容易理解了>这篇文章时,发现了一个用 Java Swing 编写的可视化算法工程,真心不错!包含了常用数据结构和算法的动态演示,先来张图感受下: 可 ...

  4. Exceptionless - 本地搭建

    搭建环境:Windows 10 参与文档:https://github.com/exceptionless/Exceptionless/wiki/Self-Hosting 运行环境: .NET 4.6 ...

  5. 2-kong的preserve_host和strip_uri解析

    原文参考:https://www.cnblogs.com/mentalidade/p/6847004.html preserve_host:当代理的时候,k代理时,Kong的默认行为是将上游请求的Ho ...

  6. python2.7写的图形密码生成器

    #coding:utf8import random,wxdef password(event): a = [chr(i) for i in range(97,123)] b = [chr(i) for ...

  7. Gin-Go学习笔记四:Gin-Web框架 文件的上传下载

    文件的上传和下载 1->文件的上传 文件的上传,采用的是uploadify.js这个插件. 本事例实现的是上传图片文件,其他的文件上传也一样. 2->文件的下载 文件的下载有两个实现的方式 ...

  8. 【译】使用WebDriver采样器将JMeter与Selenium集成

    原为地址:https://dev.to/raghwendrasonu/jmeter-integration-with-selenium-using-webdriver-sampler-176k 第一步 ...

  9. json方式的面向对象、拖拽

    //json方式的面向对象 var obj= { a:, b:, c:function(){ alert( } } obj.c();//12 //命名空间 var miaov={}; miaov.co ...

  10. 写 React / Vue 项目时为什么要在列表组件中写 key,其作用是什么

    怼一波,在项目中有很多经常用到,但又含糊不清的知识点 框架中的key: 1. 为啥在遍历元素时要用 key :在开发过程中为了保证遍历同级元素的唯一性,用来提高更新 dom 的性能: 2. 凭啥要保证 ...