创建表和关系

  1. /*
  2. 创建表
  3. */
  4.  
  5. /*年级表*/
  6. DROP TABLE IF EXISTS `class_grade`;
  7. CREATE TABLE `class_grade` (
  8. `gid` int(11) NOT NULL AUTO_INCREMENT,
  9. `gname` varchar(32) NOT NULL,
  10. PRIMARY KEY (`gid`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  12.  
  13. /*班级表*/
  14. DROP TABLE IF EXISTS `class`;
  15. CREATE TABLE `class` (
  16. `cid` int(11) NOT NULL AUTO_INCREMENT,
  17. `caption` varchar(32) NOT NULL,
  18. `grade_id` int(11) NOT NULL,
  19. PRIMARY KEY (`cid`),
  20. KEY `fk_class_grade` (`grade_id`),
  21. CONSTRAINT `fk_class_grade` FOREIGN KEY (`grade_id`) REFERENCES `class_grade` (`gid`)
  22. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  23.  
  24. /*老师表*/
  25. DROP TABLE IF EXISTS `teacher`;
  26. CREATE TABLE `teacher` (
  27. `tid` int(11) NOT NULL AUTO_INCREMENT,
  28. `tname` varchar(32) NOT NULL,
  29. PRIMARY KEY (`tid`)
  30. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  31.  
  32. /*课程表*/
  33. DROP TABLE IF EXISTS `course`;
  34. CREATE TABLE `course` (
  35. `cid` int(11) NOT NULL AUTO_INCREMENT,
  36. `cname` varchar(32) NOT NULL,
  37. `teacher_id` int(11) NOT NULL,
  38. PRIMARY KEY (`cid`),
  39. KEY `fk_course_teacher` (`teacher_id`),
  40. CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
  41. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  42.  
  43. /*学生表*/
  44. DROP TABLE IF EXISTS `student`;
  45. CREATE TABLE `student` (
  46. `sid` int(11) NOT NULL AUTO_INCREMENT,
  47. `gender` char(1) NOT NULL,
  48. `class_id` int(11) NOT NULL,
  49. `sname` varchar(32) NOT NULL,
  50. PRIMARY KEY (`sid`),
  51. KEY `fk_class` (`class_id`),
  52. CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
  53. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  54.  
  55. /*成绩表*/
  56. DROP TABLE IF EXISTS `score`;
  57. CREATE TABLE `score` (
  58. `sid` int(11) NOT NULL AUTO_INCREMENT,
  59. `student_id` int(11) NOT NULL,
  60. `course_id` int(11) NOT NULL,
  61. `score` int(11) NOT NULL,
  62. PRIMARY KEY (`sid`),
  63. KEY `fk_score_student` (`student_id`),
  64. KEY `fk_score_course` (`course_id`),
  65. CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  66. CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
  67. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  68.  
  69. /*班级任职表:*/
  70. DROP TABLE IF EXISTS `teach2cls`;
  71. CREATE TABLE `teach2cls`(
  72. `tcid` int(11) NOT NULL AUTO_INCREMENT,
  73. `tid` int(11) NOT NULL,
  74. `cid` int(11) NOT NULL,
  75. PRIMARY KEY (`tcid`),
  76. KEY `fk_teach2cls_class` (`cid`),
  77. KEY `fk_teach2cls_teacher` (`tid`),
  78. CONSTRAINT `fk_teach2cls_class` FOREIGN KEY (`cid`) REFERENCES `class` (`cid`),
  79. CONSTRAINT `fk_teach2cls_teacher` FOREIGN KEY (`tid`) REFERENCES `teacher` (`tid`)
  80. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  81.  
  82. /*
  83. 验证创建表和数据
  84. */
  85.  
  86. mysql> use db4;
  87. Database changed
  88. mysql> desc class;
  89. +----------+-------------+------+-----+---------+----------------+
  90. | Field | Type | Null | Key | Default | Extra |
  91. +----------+-------------+------+-----+---------+----------------+
  92. | cid | int(11) | NO | PRI | NULL | auto_increment |
  93. | caption | varchar(32) | NO | | NULL | |
  94. | grade_id | int(11) | NO | MUL | NULL | |
  95. +----------+-------------+------+-----+---------+----------------+
  96. 3 rows in set (0.01 sec)
  97.  
  98. mysql> select * from class;
  99. +-----+--------------+----------+
  100. | cid | caption | grade_id |
  101. +-----+--------------+----------+
  102. | 1 | 一年一班 | 1 |
  103. | 2 | 二年一班 | 2 |
  104. | 3 | 三年二班 | 3 |
  105. | 4 | 二年二班 | 2 |
  106. +-----+--------------+----------+
  107. 4 rows in set (0.00 sec)
  108.  
  109. mysql>
  110. mysql> desc student;
  111. +----------+-------------+------+-----+---------+----------------+
  112. | Field | Type | Null | Key | Default | Extra |
  113. +----------+-------------+------+-----+---------+----------------+
  114. | sid | int(11) | NO | PRI | NULL | auto_increment |
  115. | gender | char(1) | NO | | NULL | |
  116. | class_id | int(11) | NO | MUL | NULL | |
  117. | sname | varchar(32) | NO | | NULL | |
  118. +----------+-------------+------+-----+---------+----------------+
  119. 4 rows in set (0.01 sec)
  120.  
  121. mysql> select * from student limit 5;
  122. +-----+--------+----------+-----------+
  123. | sid | gender | class_id | sname |
  124. +-----+--------+----------+-----------+
  125. | 1 | | 1 | 乔丹 |
  126. | 2 | | 1 | 艾弗森 |
  127. | 3 | | 2 | 科比 |
  128. | 4 | | 1 | 张一 |
  129. | 5 | | 1 | 张二 |
  130. +-----+--------+----------+-----------+
  131. 5 rows in set (0.00 sec)
  132.  
  133. mysql> desc teacher;
  134. +-------+-------------+------+-----+---------+----------------+
  135. | Field | Type | Null | Key | Default | Extra |
  136. +-------+-------------+------+-----+---------+----------------+
  137. | tid | int(11) | NO | PRI | NULL | auto_increment |
  138. | tname | varchar(32) | NO | | NULL | |
  139. +-------+-------------+------+-----+---------+----------------+
  140. 2 rows in set (0.01 sec)
  141.  
  142. mysql> select * from teacher;
  143. +-----+-----------+
  144. | tid | tname |
  145. +-----+-----------+
  146. | 1 | 张三 |
  147. | 2 | 李四 |
  148. | 3 | 王五 |
  149. | 4 | 朱云海 |
  150. | 5 | 李杰 |
  151. +-----+-----------+
  152. 5 rows in set (0.00 sec)
  153.  
  154. mysql> desc course;
  155. +------------+-------------+------+-----+---------+----------------+
  156. | Field | Type | Null | Key | Default | Extra |
  157. +------------+-------------+------+-----+---------+----------------+
  158. | cid | int(11) | NO | PRI | NULL | auto_increment |
  159. | cname | varchar(32) | NO | | NULL | |
  160. | teacher_id | int(11) | NO | MUL | NULL | |
  161. +------------+-------------+------+-----+---------+----------------+
  162. 3 rows in set (0.01 sec)
  163.  
  164. mysql> select * from course;
  165. +-----+--------+------------+
  166. | cid | cname | teacher_id |
  167. +-----+--------+------------+
  168. | 1 | 生物 | 1 |
  169. | 2 | 体育 | 1 |
  170. | 3 | 物理 | 2 |
  171. | 4 | 美术 | 2 |
  172. +-----+--------+------------+
  173. 4 rows in set (0.00 sec)
  174.  
  175. mysql> desc score;
  176. +------------+---------+------+-----+---------+----------------+
  177. | Field | Type | Null | Key | Default | Extra |
  178. +------------+---------+------+-----+---------+----------------+
  179. | sid | int(11) | NO | PRI | NULL | auto_increment |
  180. | student_id | int(11) | NO | MUL | NULL | |
  181. | course_id | int(11) | NO | MUL | NULL | |
  182. | score | int(11) | NO | | NULL | |
  183. +------------+---------+------+-----+---------+----------------+
  184. 4 rows in set (0.01 sec)
  185.  
  186. mysql> select * from score limit 5;
  187. +-----+------------+-----------+-------+
  188. | sid | student_id | course_id | score |
  189. +-----+------------+-----------+-------+
  190. | 1 | 1 | 1 | 60 |
  191. | 2 | 1 | 2 | 59 |
  192. | 3 | 2 | 2 | 99 |
  193. | 6 | 2 | 1 | 8 |
  194. | 8 | 2 | 3 | 68 |
  195. +-----+------------+-----------+-------+
  196. 5 rows in set (0.00 sec)
  197.  
  198. mysql> desc class_grade;
  199. +-------+-------------+------+-----+---------+----------------+
  200. | Field | Type | Null | Key | Default | Extra |
  201. +-------+-------------+------+-----+---------+----------------+
  202. | gid | int(11) | NO | PRI | NULL | auto_increment |
  203. | gname | varchar(32) | NO | | NULL | |
  204. +-------+-------------+------+-----+---------+----------------+
  205. 2 rows in set (0.01 sec)
  206.  
  207. mysql> select * from class_grade;
  208. +-----+-----------+
  209. | gid | gname |
  210. +-----+-----------+
  211. | 1 | 一年级 |
  212. | 2 | 二年级 |
  213. | 3 | 三年级 |
  214. +-----+-----------+
  215. 3 rows in set (0.00 sec)
  216.  
  217. mysql> desc teach2cls;
  218. +-------+---------+------+-----+---------+----------------+
  219. | Field | Type | Null | Key | Default | Extra |
  220. +-------+---------+------+-----+---------+----------------+
  221. | tcid | int(11) | NO | PRI | NULL | auto_increment |
  222. | tid | int(11) | NO | MUL | NULL | |
  223. | cid | int(11) | NO | MUL | NULL | |
  224. +-------+---------+------+-----+---------+----------------+
  225. 3 rows in set (0.01 sec)
  226.  
  227. mysql> select * from teach2cls;
  228. +------+-----+-----+
  229. | tcid | tid | cid |
  230. +------+-----+-----+
  231. | 1 | 1 | 1 |
  232. | 2 | 1 | 2 |
  233. | 3 | 2 | 1 |
  234. | 4 | 3 | 2 |
  235. +------+-----+-----+
  236. 4 rows in set (0.00 sec)

  

创建数据与查询

  1. /*1、自行创建测试数据;*/
  2. /*
  3. 初始化数据
  4. */
  5.  
  6. BEGIN;
  7. INSERT INTO `class_grade` VALUES ('', '一年级'), ('', '二年级'), ('', '三年级');
  8. COMMIT;
  9.  
  10. BEGIN;
  11. INSERT INTO `class` VALUES ('', '一年一班',''), ('', '二年一班',''), ('', '三年二班',''), ('', '二年二班','');
  12. COMMIT;
  13.  
  14. BEGIN;
  15. INSERT INTO `teacher` VALUES ('', '张三'), ('', '李四'), ('', '王五'), ('', '朱云海'), ('', '李杰');
  16. COMMIT;
  17.  
  18. BEGIN;
  19. INSERT INTO `student` VALUES ('', '女', '', '乔丹'), ('', '女', '', '艾弗森'), ('', '男', '', '科比'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四');
  20. COMMIT;
  21.  
  22. BEGIN;
  23. INSERT INTO `course` VALUES ('', '生物', ''), ('', '体育', ''),('', '物理', ''), ('', '美术', ''),('', '语文', ''),('', '数学', ''),('', '英语', ''),('', '化学', '');
  24. COMMIT;
  25.  
  26. BEGIN;
  27. INSERT INTO `teach2cls` VALUES ('', '',''), ('', '',''), ('', '',''), ('', '',''),('', '','');
  28. COMMIT;
  29.  
  30. BEGIN;
  31. INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''),
  32. ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
  33. COMMIT;
  34.  
  35. /*2、查询学生总人数;*/
  36. select count(sid) as '学生总人数' from student
  37.  
  38. /*3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;*/
  39. SELECT
  40. student.sid as '学生id', student.sname as '姓名'
  41. FROM
  42. student
  43. WHERE
  44. student.sid IN (SELECT
  45. student_id
  46. FROM
  47. score
  48. INNER JOIN
  49. course ON course.cid = score.course_id
  50. WHERE
  51. score >= 60
  52. AND course.cname IN ('生物' , '物理'))
  53.  
  54. /*4、查询每个年级的班级数,取出班级数最多的前三个年级;*/
  55. SELECT
  56. t1.gid, t1.gname as '年级名称'
  57. FROM
  58. (SELECT
  59. grade_id, COUNT(cid) cid_num
  60. FROM
  61. class
  62. GROUP BY class.grade_id
  63. ORDER BY cid_num DESC
  64. LIMIT 3) t2
  65. INNER JOIN
  66. class_grade t1 ON t2.grade_id = t1.gid;
  67.  
  68. /*5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;*/
  69. SELECT
  70. student.sid as '学生id', student.sname as '姓名', avg_score as '平均成绩'
  71. FROM
  72. student
  73. INNER JOIN
  74. (SELECT
  75. student_id, avg_score
  76. FROM
  77. (SELECT
  78. student_id, AVG(score) AS avg_score
  79. FROM
  80. score
  81. GROUP BY student_id
  82. ORDER BY AVG(score) DESC
  83. LIMIT 1) AS t1 UNION (SELECT
  84. student_id, AVG(score) AS avg_score
  85. FROM
  86. score
  87. GROUP BY student_id
  88. ORDER BY AVG(score)
  89. LIMIT 1)) t2 ON student.sid = t2.student_id
  90.  
  91. /*6、查询每个年级的学生人数;*/
  92. SELECT
  93. gid, COUNT(student.sid) as '学生人数'
  94. FROM
  95. class_grade
  96. LEFT JOIN
  97. class ON class.grade_id = gid
  98. LEFT JOIN
  99. student ON student.class_id = class.cid
  100. GROUP BY gid;
  101.  
  102. /*7、查询每位学生的学号,姓名,选课数,平均成绩;*/
  103. SELECT
  104. student.sid as '学号', student.sname as '姓名', IFNULL(avg_score,0) '平均成绩', IFNULL(course_num,0) '选课数'
  105. FROM
  106. student
  107. LEFT JOIN
  108. (SELECT
  109. student_id,
  110. AVG(score) avg_score,
  111. COUNT(course_id) course_num
  112. FROM
  113. score
  114. GROUP BY student_id) AS t1 ON student.sid = t1.student_id
  115.  
  116. /*8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;*/
  117. SELECT
  118. student.sname as '学生姓名', course.cname as '课程名', t2.score as '分数'
  119. FROM
  120. (SELECT
  121. student_id, course_id, score
  122. FROM
  123. (SELECT
  124. student_id, course_id, score
  125. FROM
  126. score
  127. WHERE
  128. student_id = ''
  129. ORDER BY score DESC
  130. LIMIT 1) t1 UNION (SELECT
  131. student_id, course_id, score
  132. FROM
  133. score
  134. WHERE
  135. student_id = ''
  136. ORDER BY score
  137. LIMIT 1)) t2
  138. INNER JOIN
  139. student ON sid = t2.student_id
  140. INNER JOIN
  141. course ON cid = t2.course_id
  142.  
  143. /*9、查询姓“李”的老师的个数和所带班级数;*/
  144. SELECT
  145. t1.tid, tid_num as '个数', IFNULL(cid_num, 0) as '班级数'
  146. FROM
  147. (SELECT
  148. tid, COUNT(tid) tid_num
  149. FROM
  150. teacher
  151. WHERE
  152. tname LIKE '李%'
  153. GROUP BY tid) t1
  154. LEFT JOIN
  155. (SELECT
  156. tid, COUNT(cid) cid_num
  157. FROM
  158. teach2cls
  159. GROUP BY tid) t2 ON t2.tid = t1.tid
  160.  
  161. /*10、查询班级数小于5的年级id和年级名;*/
  162. SELECT
  163. t1.gid as '年级id' , t1.gname as '年级名'
  164. FROM
  165. (SELECT
  166. grade_id, COUNT(cid) cid_num
  167. FROM
  168. class
  169. GROUP BY class.grade_id
  170. HAVING COUNT(cid) < 5) t2
  171. INNER JOIN
  172. class_grade t1 ON t2.grade_id = t1.gid;
  173.  
  174. /* 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果
  175. 如下;
  176. */
  177. SELECT
  178. cid as '班级id', caption as '班级名称', t1.gname as '年级' , t1.gid_level as '年级级别'
  179. FROM
  180. class
  181. INNER JOIN
  182. (SELECT
  183. t.gid,
  184. t.gname,
  185. (CASE
  186. WHEN t.gid IN ('' , '') THEN '低年级'
  187. WHEN t.gid IN ('' , '') THEN '中年级'
  188. WHEN t.gid IN ('' , '') THEN '高年级'
  189. END) gid_level
  190. FROM
  191. class_grade t) AS t1 ON t1.gid = class.grade_id
  192.  
  193. /*12、查询学过“张三”老师2门课以上的同学的学号、姓名;*/
  194. SELECT
  195. sid '学号', sname '姓名'
  196. FROM
  197. student
  198. WHERE
  199. sid IN (SELECT
  200. student_id
  201. FROM
  202. score
  203. WHERE
  204. course_id IN (SELECT
  205. cid
  206. FROM
  207. course
  208. WHERE
  209. teacher_id = (SELECT
  210. tid
  211. FROM
  212. teacher
  213. WHERE
  214. tname = '张三'))
  215. GROUP BY student_id
  216. HAVING COUNT(student_id) >= 2)
  217.  
  218. /*13、查询教授课程超过2门的老师的id和姓名;*/
  219. SELECT
  220. tid as '老师id', tname as '老师姓名'
  221. FROM
  222. teacher
  223. WHERE
  224. tid IN (SELECT
  225. teacher_id
  226. FROM
  227. course
  228. GROUP BY teacher_id
  229. HAVING COUNT(teacher_id) > 2)
  230.  
  231. /* 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;*/
  232. SELECT
  233. student.sid '学号', student.sname '姓名'
  234. FROM
  235. student
  236. WHERE
  237. student.sid IN (SELECT
  238. student_id
  239. FROM
  240. score
  241. WHERE
  242. course_id IN ('' , ''))
  243.  
  244. /*15、查询没有带过高年级的老师id和姓名;*/
  245. SELECT
  246. tid '老师id', tname '姓名'
  247. FROM
  248. teacher
  249. WHERE
  250. tid IN (SELECT
  251. tid
  252. FROM
  253. teach2cls
  254. WHERE
  255. cid IN (SELECT
  256. cid
  257. FROM
  258. class
  259. WHERE
  260. grade_id IN (SELECT
  261. t.gid
  262. FROM
  263. class_grade t
  264. WHERE
  265. t.gid NOT IN ('' , ''))))
  266.  
  267. /*16、查询学过“张三”老师所教的所有课的同学的学号、姓名;*/
  268. SELECT
  269. sid '学号', sname '姓名'
  270. FROM
  271. student
  272. WHERE
  273. sid IN (SELECT
  274. student_id
  275. FROM
  276. score
  277. INNER JOIN
  278. (SELECT
  279. cid
  280. FROM
  281. course
  282. WHERE
  283. teacher_id = (SELECT
  284. tid
  285. FROM
  286. teacher
  287. WHERE
  288. tname = '张三')) t1 ON t1.cid = score.course_id
  289. GROUP BY student_id
  290. HAVING COUNT(student_id) = (SELECT
  291. COUNT(cid)
  292. FROM
  293. course
  294. WHERE
  295. teacher_id = (SELECT
  296. tid
  297. FROM
  298. teacher
  299. WHERE
  300. tname = '张三')))
  301.  
  302. /*17、查询带过超过2个班级的老师的id和姓名;*/
  303. SELECT
  304. tid '老师id', tname '姓名'
  305. FROM
  306. teacher
  307. WHERE
  308. tid IN (SELECT
  309. tid
  310. FROM
  311. teach2cls
  312. GROUP BY tid
  313. HAVING COUNT(tid) > 2)
  314.  
  315. /*18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;*/
  316.  
  317. SELECT
  318. student.sid '学号', student.sname '姓名'
  319. FROM
  320. student
  321. WHERE
  322. student.sid IN (SELECT
  323. t2.student_id
  324. FROM
  325. (SELECT
  326. student_id, score score_1
  327. FROM
  328. score
  329. WHERE
  330. course_id = '') t1
  331. INNER JOIN
  332. (SELECT
  333. student_id, score score_2
  334. FROM
  335. score
  336. WHERE
  337. course_id = '') t2 ON t1.student_id = t2.student_id
  338. WHERE
  339. t2.score_2 < t1.score_1)
  340.  
  341. /*19、查询所带班级数最多的老师id和姓名;*/
  342. SELECT
  343. tid '老师id', tname '姓名'
  344. FROM
  345. teacher
  346. WHERE
  347. tid IN (SELECT
  348. tid
  349. FROM
  350. teach2cls
  351. GROUP BY tid
  352. HAVING COUNT(tid) = (SELECT
  353. MAX(t1.tid_num)
  354. FROM
  355. (SELECT
  356. tid, COUNT(tid) tid_num
  357. FROM
  358. teach2cls
  359. GROUP BY tid) t1))
  360.  
  361. /*20、查询有课程成绩小于60分的同学的学号、姓名;*/
  362. SELECT
  363. student.sid '学号', student.sname '姓名'
  364. FROM
  365. student
  366. WHERE
  367. student.sid IN (SELECT
  368. student_id
  369. FROM
  370. score
  371. WHERE
  372. score < 60)
  373.  
  374. /*21、查询没有学全所有课的同学的学号、姓名;*/
  375. SELECT
  376. student.sid '学号', student.sname '姓名'
  377. FROM
  378. student
  379. WHERE
  380. student.sid NOT IN (SELECT
  381. student_id
  382. FROM
  383. score
  384. GROUP BY student_id
  385. HAVING COUNT(student_id) = (SELECT
  386. COUNT(cid)
  387. FROM
  388. course))
  389.  
  390. /*22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;*/
  391. SELECT
  392. student.sid '学号', student.sname '姓名'
  393. FROM
  394. student
  395. WHERE
  396. student.sid IN (SELECT
  397. student_id
  398. FROM
  399. score
  400. WHERE
  401. student_id <> ''
  402. AND course_id IN (SELECT
  403. course_id
  404. FROM
  405. score
  406. WHERE
  407. student_id = ''))
  408.  
  409. /*23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;*/
  410. SELECT
  411. student.sid '学号', student.sname '姓名'
  412. FROM
  413. student
  414. WHERE
  415. student.sid IN (SELECT
  416. student_id
  417. FROM
  418. score
  419. WHERE
  420. student_id <> ''
  421. AND course_id IN (SELECT
  422. course_id
  423. FROM
  424. score
  425. WHERE
  426. student_id = ''))
  427.  
  428. /*24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;*/
  429.  
  430. SELECT
  431. student.sid '学号', student.sname '姓名'
  432. FROM
  433. student
  434. WHERE
  435. student.sid IN (SELECT
  436. student_id
  437. FROM
  438. score
  439. WHERE
  440. student_id <> ''
  441. AND course_id IN (SELECT
  442. course_id
  443. FROM
  444. score
  445. WHERE
  446. student_id = '')
  447. GROUP BY student_id
  448. HAVING COUNT(student_id) = (SELECT
  449. COUNT(course_id)
  450. FROM
  451. score
  452. WHERE
  453. student_id = ''))
  454.  
  455. /*25、删除学习“张三”老师课的score表记录;*/
  456. DELETE FROM score
  457. WHERE
  458. course_id IN (SELECT
  459. cid
  460. FROM
  461. course
  462.  
  463. WHERE
  464. teacher_id = (SELECT
  465. tid
  466. FROM
  467. teacher
  468.  
  469. WHERE
  470. tname = '张三'));
  471.  
  472. /*26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课
  473. 程的平均成绩;
  474. */
  475.  
  476. ①没有上过编号“2”课程的同学学号
  477. SELECT
  478. student.sid
  479. FROM
  480. student
  481. WHERE
  482. student.sid not in(
  483. SELECT
  484. student_id
  485. FROM
  486. score
  487. WHERE
  488. course_id in (''))
  489.  
  490. ②插入“2”号课程的平均成绩;
  491.  
  492. SELECT
  493. AVG(score)
  494. FROM
  495. score
  496. WHERE
  497. course_id = ''
  498.  
  499. INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''),('', '', '', '');
  500. commit;
  501.  
  502. /*27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,
  503. 数学,英语,有效课程数,有效平均分;*/
  504. SELECT
  505. student_id AS '学生ID',
  506. IFNULL(AVG(CASE
  507. WHEN course.cname = '语文' THEN score
  508. END),
  509. 0) AS '语文',
  510. IFNULL(AVG(CASE
  511. WHEN course.cname = '数学' THEN score
  512. END),
  513. 0) AS '数学',
  514. IFNULL(AVG(CASE
  515. WHEN course.cname = '英语' THEN score
  516. END),
  517. 0) AS '英语',
  518. COUNT(student_id) AS '有效课程数',
  519. AVG(score) AS '有效平均分'
  520. FROM
  521. score
  522. INNER JOIN
  523. course ON course.cid = score.course_id
  524. GROUP BY student_id
  525. ORDER BY AVG(score)
  526.  
  527. /*28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;*/
  528. SELECT
  529. course_id AS '课程ID',
  530. MAX(score) AS '最高分',
  531. MIN(score) AS '最低分'
  532. FROM
  533. score
  534. GROUP BY course_id;
  535.  
  536. /*29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;*/
  537.  
  538. SELECT
  539. course_id AS '课程ID',
  540. AVG(score) AS '平均成绩',
  541. SUM(CASE
  542. WHEN score >= 60 THEN 1
  543. ELSE 0
  544. END) / COUNT(course_id) * 100 AS '及格率'
  545. FROM
  546. score
  547. GROUP BY course_id
  548. ORDER BY AVG(score) ASC , SUM(CASE
  549. WHEN score >= 60 THEN 1
  550. ELSE 0
  551. END) / COUNT(course_id) * 100 DESC;
  552.  
  553. /*30、课程平均分从高到低显示(显示任课老师);*/
  554.  
  555. SELECT
  556. course_id AS '课程ID',
  557. AVG(score) AS '平均成绩',
  558. teacher.tname
  559. FROM
  560. score
  561. INNER JOIN
  562. course ON course_id = course.cid
  563. INNER JOIN
  564. teacher ON teacher.tid = course.teacher_id
  565. GROUP BY course_id
  566. ORDER BY AVG(score) DESC;
  567.  
  568. /*31、查询各科成绩前三名的记录(不考虑成绩并列情况) ;
  569. */
  570.  
  571. SELECT
  572. course_id, MAX(score) score
  573. FROM
  574. score
  575. GROUP BY course_id
  576. UNION (SELECT
  577. score.course_id, MAX(score) AS score
  578. FROM
  579. score
  580. INNER JOIN
  581. (SELECT
  582. course_id, MAX(score) first_num
  583. FROM
  584. score
  585. GROUP BY course_id) t1 ON score.course_id = t1.course_id
  586. WHERE
  587. score.score < t1.first_num
  588. GROUP BY score.course_id)
  589. UNION (SELECT
  590. score.course_id, MAX(score) AS score
  591. FROM
  592. score
  593. INNER JOIN
  594. (SELECT
  595. score.course_id, MAX(score) AS second_num
  596. FROM
  597. score
  598. INNER JOIN (SELECT
  599. course_id, MAX(score) first_num
  600. FROM
  601. score
  602. GROUP BY course_id) t1 ON score.course_id = t1.course_id
  603. WHERE
  604. score.score < t1.first_num
  605. GROUP BY score.course_id) t2 ON score.course_id = t2.course_id
  606. WHERE
  607. score.score < t2.second_num
  608. GROUP BY score.course_id) ORDER BY course_id , score DESC
  609.  
  610. /*32、查询每门课程被选修的学生数;
  611.  
  612. */
  613. SELECT
  614. cname '课程', IFNULL(stu_num, 0) '学生数'
  615. FROM
  616. course
  617. LEFT JOIN
  618. (SELECT
  619. course_id, COUNT(student_id) stu_num
  620. FROM
  621. score
  622. GROUP BY course_id) t1 ON course.cid = t1.course_id
  623.  
  624. /*33、查询选修了2门以上课程的全部学生的学号和姓名;*/
  625. SELECT
  626. student.sid '学号', student.sname '姓名'
  627. FROM
  628. student
  629. WHERE
  630. student.sid IN (SELECT
  631. student_id
  632. FROM
  633. score
  634. GROUP BY student_id
  635. HAVING COUNT(course_id) > 2)
  636.  
  637. /*34、查询男生、女生的人数,按倒序排列;*/
  638. SELECT
  639. gender '性别', COUNT(sid) '人数'
  640. FROM
  641. student
  642. GROUP BY gender
  643. ORDER BY COUNT(sid) DESC
  644.  
  645. /*35、查询姓“张”的学生名单;*/
  646.  
  647. SELECT
  648. sid '学号', sname '姓名'
  649. FROM
  650. student
  651. WHERE
  652. sname LIKE '张%'
  653.  
  654. /*36、查询同名同姓学生名单,并统计同名人数;
  655. */
  656. SELECT
  657. sname '学生名', COUNT(sname) '人数'
  658. FROM
  659. student
  660. GROUP BY sname
  661. HAVING COUNT(sname) > 1
  662.  
  663. /*37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
  664. */
  665. SELECT
  666. course_id '课程号' , AVG(score) '平均成绩'
  667. FROM
  668. score
  669. GROUP BY course_id
  670. ORDER BY AVG(score) ASC , course_id DESC
  671.  
  672. /*38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;*/
  673. SELECT
  674. student.sname '学生姓名', score '成绩'
  675. FROM
  676. student
  677. INNER JOIN
  678. (SELECT DISTINCT
  679. student_id, score
  680. FROM
  681. score
  682. INNER JOIN course ON course.cid = score.course_id
  683. WHERE
  684. score < 60 AND course.cname = '数学') t1 ON student.sid = t1.student_id
  685.  
  686. /*39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;*/
  687. SELECT
  688. student.sid '学号', student.sname '姓名'
  689. FROM
  690. student
  691. WHERE
  692. student.sid IN (SELECT
  693. student_id
  694. FROM
  695. score
  696. WHERE
  697. course_id = '' AND score > 80)
  698.  
  699. /*40、求选修了课程的学生人数;*/
  700. SELECT
  701. COUNT(DISTINCT student_id) '学生人数'
  702. FROM
  703. score
  704.  
  705. /*41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
  706. */
  707. SELECT
  708. sname '学生姓名', t4.score '成绩'
  709. FROM
  710. student
  711. INNER JOIN
  712. (SELECT
  713. student_id, score
  714. FROM
  715. (SELECT
  716. student_id, score
  717. FROM
  718. score
  719. INNER JOIN (SELECT
  720. cid
  721. FROM
  722. course
  723. WHERE
  724. teacher_id = (SELECT
  725. tid
  726. FROM
  727. teacher
  728. WHERE
  729. tname = '王五')) t1 ON t1.cid = score.course_id
  730. ORDER BY score DESC
  731. LIMIT 1) t2 UNION (SELECT
  732. student_id, score
  733. FROM
  734. score
  735. INNER JOIN (SELECT
  736. cid
  737. FROM
  738. course
  739. WHERE
  740. teacher_id = (SELECT
  741. tid
  742. FROM
  743. teacher
  744. WHERE
  745. tname = '王五')) t1 ON t1.cid = score.course_id
  746. ORDER BY score ASC
  747. LIMIT 1)) t4 ON t4.student_id = student.sid
  748.  
  749. /*42、查询各个课程及相应的选修人数;*/
  750. SELECT
  751. cname '课程', IFNULL(stu_num, 0) '选修人数'
  752. FROM
  753. course
  754. LEFT JOIN
  755. (SELECT
  756. t1.course_id, COUNT(t1.student_id) stu_num
  757. FROM
  758. score t1
  759. GROUP BY t1.course_id) t2 ON course.cid = t2.course_id
  760.  
  761. /*43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;*/
  762. SELECT DISTINCT
  763. t1.student_id '学号', t1.course_id '课程号', t1.score '学生成绩'
  764. FROM
  765. score t1
  766. INNER JOIN
  767. score ON t1.student_id = score.student_id
  768. WHERE
  769. t1.course_id <> score.course_id
  770. AND t1.score = score.score
  771.  
  772. /*44、查询每门课程成绩最好的前两名学生id和姓名;*/
  773.  
  774. SELECT
  775. student.sid '学号', student.sname '姓名'
  776. FROM
  777. student
  778. WHERE
  779. student.sid IN (SELECT
  780. score.student_id
  781. FROM
  782. score
  783. INNER JOIN
  784. (SELECT
  785. t3.course_id, first_num, second_num
  786. FROM
  787. (SELECT
  788. course_id, MAX(score) first_num
  789. FROM
  790. score
  791. GROUP BY course_id) t3
  792. INNER JOIN (SELECT
  793. score.course_id, MAX(score) AS second_num
  794. FROM
  795. score
  796. INNER JOIN (SELECT
  797. course_id, MAX(score) first_num
  798. FROM
  799. score
  800. GROUP BY course_id) t1 ON score.course_id = t1.course_id
  801. WHERE
  802. score.score < t1.first_num
  803. GROUP BY score.course_id) t2 ON t3.course_id = t2.course_id) t4 ON score.course_id = t4.course_id
  804. WHERE
  805. score.score <= t4.first_num
  806. AND score.score >= t4.second_num)
  807.  
  808. /*45、检索至少选修两门课程的学生学号;*/
  809. SELECT
  810. student_id
  811. FROM
  812. score
  813. GROUP BY student_id
  814. HAVING COUNT(course_id) >= 2;
  815.  
  816. /*46、查询没有学生选修的课程的课程号和课程名;*/
  817. SELECT
  818. cid '课程号', cname '课程名'
  819. FROM
  820. course
  821. WHERE
  822. cid NOT IN (SELECT DISTINCT
  823. course_id
  824. FROM
  825. score)
  826.  
  827. /*47、查询没带过任何班级的老师id和姓名;*/
  828. SELECT
  829. tid '老师id', tname '姓名'
  830. FROM
  831. teacher
  832. WHERE
  833. tid NOT IN (SELECT
  834. tid
  835. FROM
  836. teach2cls)
  837.  
  838. /*48、查询有两门以上课程超过80分的学生id及其平均成绩*/
  839. SELECT
  840. student_id '学号', AVG(score) '平均成绩'
  841. FROM
  842. score
  843. WHERE
  844. score > 80
  845. GROUP BY student_id
  846. HAVING COUNT(course_id) > 2
  847.  
  848. /*49、检索“3”课程分数小于60,按分数降序排列的同学学号;*/
  849. SELECT
  850. student_id '学号'
  851. FROM
  852. score
  853. WHERE
  854. course_id = '' AND score < 60
  855. ORDER BY score DESC;
  856.  
  857. /*50、删除编号为“2”的同学的“1”课程的成绩;*/
  858.  
  859. delete from score where student_id='' and course_id='';
  860.  
  861. /*51、查询同时选修了物理课和生物课的学生id和姓名。*/
  862. SELECT
  863. student.sid '学号', student.sname '姓名'
  864. FROM
  865. student
  866. WHERE
  867. student.sid IN (SELECT
  868. student_id
  869. FROM
  870. score
  871. INNER JOIN
  872. course ON course.cid = score.course_id
  873. WHERE
  874. course.cname IN ('生物' , '物理')
  875. GROUP BY student_id
  876. HAVING COUNT(course_id) = 2)

MySQL查询50例的更多相关文章

  1. PHP/MYSQL 查询大数据/遍历表

    PHP:PHP 5.3.6 (cli) (built: Jun 15 2011 16:29:50) MYSQL:5.1.51 如果我们有的一张表有几百万或几千万的记录,我们要使用 PHP 将所有的记录 ...

  2. MYSQL查询语句大全集锦

    MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...

  3. mysql查询正在执行的进程

    查看mysql进程有两种方法 1.进入mysql/bin目录下输入mysqladmin processlist; 2.启动mysql,输入show processlist; 如果有SUPER权限,则可 ...

  4. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  5. [转]向facebook学习,通过协程实现mysql查询的异步化

    FROM : 通过协程实现mysql查询的异步化 前言 最近学习了赵海平的演讲,了解到facebook的mysql查询可以进行异步化,从而提高性能.由于facebook实现的比较早,他们不得不对php ...

  6. mysql 查询随机条记录的sql语句和php计算概率

    最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来. SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS id FROM ...

  7. mysql查询更新时的锁表机制分析

    为了给高并发情况下的mysql进行更好的优化,有必要了解一下mysql查询更新时的锁表机制. 一.概述 MySQL有三种锁的级别:页级.表级.行级.MyISAM和MEMORY存储引擎采用的是表级锁(t ...

  8. mysql show processlist 显示mysql查询进程

    1.进入mysql/bin目录下输入mysqladmin processlist; 2.启动mysql,输入show processlist; 如果有 SUPER 权限,则可以看到全部的线程,否则,只 ...

  9. mysql查询缓存打开、设置、参数查询、性能变量意思

    http://blog.sina.com.cn/s/blog_75ad10100101by7j.html http://www.cnblogs.com/zemliu/archive/2013/08/0 ...

随机推荐

  1. zookeeper 单机集成部署

    概述 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等,是很多分布式的基础设置,比如dubbo,k ...

  2. (10)The secret to great opportunities? The person you haven't met yet

    https://www.ted.com/talks/tanya_menon_the_secret_to_great_opportunities_the_person_you_haven_t_met_y ...

  3. Lagrange 乘子法求最优解

    clc clear syms x y z r1 r2 w f=x^+y^+z^+w^; g1=*x-y+z-w-; g2=x+y-z+w-; h=f-r1*g1 -r2*g2; hx=diff(h,x ...

  4. PHP源码编译安装

    cd php-5.6.0yum -y install libcurl-devel bzip2-devel zlib-devel libjpeg-devel libpng-devel freetype- ...

  5. Node简单的控制台读取和文件操作

    const fs = require('fs'); const readline = require('readline'); const rl = readline.createInterface( ...

  6. C#创建、设置和安装Windows服务

    文章大部分内容转自:http://www.cnblogs.com/greatandforever/archive/2008/10/14/1310504.html:和:http://www.cnblog ...

  7. Arcgis Server Manager发布ArcGISTiledMapServiceLayer服务

    用ArcgisServer manager发布一个ArcGISDynamicMapServiceLayer服务: Add New Service->填写name,type 选择Map servi ...

  8. 回文(palindrome)

    如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文).

  9. android-基础编程-RecyclerView

    以后android-基础编程*都是控件demo里面的,不再累赘重写.直接介绍控件使用. RecyclerView is a more advanced and flexible version of ...

  10. 【repost】对JAVASCRIPT匿名函数的理解(透彻版)

    Query片段: view plaincopy to clipboardprint? (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其 ...