{"id":215,"date":"2024-06-17T21:20:52","date_gmt":"2024-06-17T13:20:52","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=215"},"modified":"2024-06-17T23:57:13","modified_gmt":"2024-06-17T15:57:13","slug":"%e5%ae%9e%e9%aa%8c%ef%bc%9asql%e7%bb%bc%e5%90%88%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=215","title":{"rendered":"\u5b9e\u9a8c\uff1aSQL\u7efc\u5408\u7ec3\u4e60"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"bvsKO\">\u7efc\u5408\u7ec3\u4e60\uff1a\u8868\u7ed3\u6784\u4e0e\u6570\u636e\u5bfc\u5165<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wUIeG\"><code>init.sql<\/code> \u6587\u4ef6\u5185\u5bb9<\/h3>\n\n\n\n<pre id=\"366e8972\" class=\"wp-block-code\"><code>\/*\n \u6570\u636e\u5bfc\u5165\uff1a\n Navicat Premium Data Transfer\n\n Source Server         : localhost\n Source Server Type    : MySQL\n Source Server Version : 50624\n Source Host           : localhost\n Source Database       : sqlexam\n\n Target Server Type    : MySQL\n Target Server Version : 50624\n File Encoding         : utf-8\n\n Date: 10\/21\/2016 06:46:46 AM\n*\/\n\nSET NAMES utf8;\nSET FOREIGN_KEY_CHECKS = 0;\n\n-- ----------------------------\n--  Table structure for `class`\n-- ----------------------------\nDROP TABLE IF EXISTS `class`;\nCREATE TABLE `class` (\n  `cid` int(11) NOT NULL AUTO_INCREMENT,\n  `caption` varchar(32) NOT NULL,\n  PRIMARY KEY (`cid`)\n) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `class`\n-- ----------------------------\nBEGIN;\nINSERT INTO `class` VALUES ('1', '\u4e09\u5e74\u4e8c\u73ed'), ('2', '\u4e09\u5e74\u4e09\u73ed'), ('3', '\u4e00\u5e74\u4e8c\u73ed'), ('4', '\u4e8c\u5e74\u4e5d\u73ed');\nCOMMIT;\n\n-- ----------------------------\n--  Table structure for `course`\n-- ----------------------------\nDROP TABLE IF EXISTS `course`;\nCREATE TABLE `course` (\n  `cid` int(11) NOT NULL AUTO_INCREMENT,\n  `cname` varchar(32) NOT NULL,\n  `teacher_id` int(11) NOT NULL,\n  PRIMARY KEY (`cid`),\n  KEY `fk_course_teacher` (`teacher_id`),\n  CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)\n) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `course`\n-- ----------------------------\nBEGIN;\nINSERT INTO `course` VALUES ('1', '\u751f\u7269', '1'), ('2', '\u7269\u7406', '2'), ('3', '\u4f53\u80b2', '3'), ('4', '\u7f8e\u672f', '2');\nCOMMIT;\n\n-- ----------------------------\n--  Table structure for `score`\n-- ----------------------------\nDROP TABLE IF EXISTS `score`;\nCREATE TABLE `score` (\n  `sid` int(11) NOT NULL AUTO_INCREMENT,\n  `student_id` int(11) NOT NULL,\n  `course_id` int(11) NOT NULL,\n  `num` int(11) NOT NULL,\n  PRIMARY KEY (`sid`),\n  KEY `fk_score_student` (`student_id`),\n  KEY `fk_score_course` (`course_id`),\n  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),\n  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)\n) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `score`\n-- ----------------------------\nBEGIN;\nINSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');\nCOMMIT;\n\n-- ----------------------------\n--  Table structure for `student`\n-- ----------------------------\nDROP TABLE IF EXISTS `student`;\nCREATE TABLE `student` (\n  `sid` int(11) NOT NULL AUTO_INCREMENT,\n  `gender` char(1) NOT NULL,\n  `class_id` int(11) NOT NULL,\n  `sname` varchar(32) NOT NULL,\n  PRIMARY KEY (`sid`),\n  KEY `fk_class` (`class_id`),\n  CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)\n) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `student`\n-- ----------------------------\nBEGIN;\nINSERT INTO `student` VALUES ('1', '\u7537', '1', '\u7406\u89e3'), ('2', '\u5973', '1', '\u94a2\u86cb'), ('3', '\u7537', '1', '\u5f20\u4e09'), ('4', '\u7537', '1', '\u5f20\u4e00'), ('5', '\u5973', '1', '\u5f20\u4e8c'), ('6', '\u7537', '1', '\u5f20\u56db'), ('7', '\u5973', '2', '\u94c1\u9524'), ('8', '\u7537', '2', '\u674e\u4e09'), ('9', '\u7537', '2', '\u674e\u4e00'), ('10', '\u5973', '2', '\u674e\u4e8c'), ('11', '\u7537', '2', '\u674e\u56db'), ('12', '\u5973', '3', '\u5982\u82b1'), ('13', '\u7537', '3', '\u5218\u4e09'), ('14', '\u7537', '3', '\u5218\u4e00'), ('15', '\u5973', '3', '\u5218\u4e8c'), ('16', '\u7537', '3', '\u5218\u56db');\nCOMMIT;\n\n-- ----------------------------\n--  Table structure for `teacher`\n-- ----------------------------\nDROP TABLE IF EXISTS `teacher`;\nCREATE TABLE `teacher` (\n  `tid` int(11) NOT NULL AUTO_INCREMENT,\n  `tname` varchar(32) NOT NULL,\n  PRIMARY KEY (`tid`)\n) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n--  Records of `teacher`\n-- ----------------------------\nBEGIN;\nINSERT INTO `teacher` VALUES ('1', '\u5f20\u78ca\u8001\u5e08'), ('2', '\u674e\u5e73\u8001\u5e08'), ('3', '\u5218\u6d77\u71d5\u8001\u5e08'), ('4', '\u6731\u4e91\u6d77\u8001\u5e08'), ('5', '\u674e\u6770\u8001\u5e08');\nCOMMIT;\n\nSET FOREIGN_KEY_CHECKS = 1;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"JXMxU\">\u5bfc\u5165\u6570\u636e<\/h3>\n\n\n\n<pre id=\"e0a52370\" class=\"wp-block-code\"><code># \u51c6\u5907\u8868\u3001\u8bb0\u5f55\nCREATE DATABASE db1;\nUSE db1;\nSOURCE \/root\/init.sql;\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/06\/1036857-20180211190041138-168655281-1024x675.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"675\" data-original=\"https:\/\/zhaoyanqi.cn\/wp-content\/uploads\/2024\/06\/1036857-20180211190041138-168655281-1024x675.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-218\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"L9DK2\">\u7ec3\u4e60<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"y7urZ\">1. <strong>\u67e5\u8be2\u6240\u6709\u7684\u8bfe\u7a0b\u7684\u540d\u79f0\u4ee5\u53ca\u5bf9\u5e94\u7684\u4efb\u8bfe\u8001\u5e08\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"5b476546\" class=\"wp-block-code\"><code>SELECT course.cname, teacher.tname \nFROM course \nJOIN teacher ON course.teacher_id = teacher.tid;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"xC9w3\">2. <strong>\u67e5\u8be2\u5b66\u751f\u8868\u4e2d\u7537\u5973\u751f\u5404\u6709\u591a\u5c11\u4eba<\/strong><\/h3>\n\n\n\n<pre id=\"9c2186b7\" class=\"wp-block-code\"><code>SELECT gender, COUNT(*) \nFROM student \nGROUP BY gender;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"jkpls\">3. <strong>\u67e5\u8be2\u7269\u7406\u6210\u7ee9\u7b49\u4e8e100\u7684\u5b66\u751f\u7684\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"e116e02a\" class=\"wp-block-code\"><code>SELECT student.sname \nFROM score \nJOIN student ON score.student_id = student.sid \nWHERE score.course_id = (SELECT cid FROM course WHERE cname = '\u7269\u7406') \n  AND score.num = 100;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Js1qW\">4. <strong>\u67e5\u8be2\u5e73\u5747\u6210\u7ee9\u5927\u4e8e\u516b\u5341\u5206\u7684\u540c\u5b66\u7684\u59d3\u540d\u548c\u5e73\u5747\u6210\u7ee9<\/strong><\/h3>\n\n\n\n<pre id=\"9b349564\" class=\"wp-block-code\"><code>SELECT student.sname, AVG(score.num) as avg_score \nFROM score \nJOIN student ON score.student_id = student.sid \nGROUP BY student.sid \nHAVING avg_score &gt; 80;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ePuWM\">5. <strong>\u67e5\u8be2\u6240\u6709\u5b66\u751f\u7684\u5b66\u53f7\uff0c\u59d3\u540d\uff0c\u9009\u8bfe\u6570\uff0c\u603b\u6210\u7ee9<\/strong><\/h3>\n\n\n\n<pre id=\"392f08d0\" class=\"wp-block-code\"><code>SELECT student.sid, student.sname, COUNT(score.course_id) as course_count, SUM(score.num) as total_score \nFROM student \nLEFT JOIN score ON student.sid = score.student_id \nGROUP BY student.sid;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dqqA4\">6. <strong>\u67e5\u8be2\u59d3\u674e\u8001\u5e08\u7684\u4e2a\u6570<\/strong><\/h3>\n\n\n\n<pre id=\"OQYAy\" class=\"wp-block-code\"><code>SELECT COUNT(*) \nFROM teacher \nWHERE tname LIKE '\u674e%';<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"JwkVb\">7. <strong>\u67e5\u8be2\u6ca1\u6709\u62a5\u674e\u5e73\u8001\u5e08\u8bfe\u7684\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"c3c3ef73\" class=\"wp-block-code\"><code>SELECT sname \nFROM student \nWHERE sid NOT IN (\n    SELECT student_id \n    FROM score \n    WHERE course_id IN (\n        SELECT cid \n        FROM course \n        WHERE teacher_id = (SELECT tid FROM teacher WHERE tname = '\u674e\u5e73\u8001\u5e08')\n    )\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dOxhC\">8. <strong>\u67e5\u8be2\u7269\u7406\u8bfe\u7a0b\u6bd4\u751f\u7269\u8bfe\u7a0b\u9ad8\u7684\u5b66\u751f\u7684\u5b66\u53f7<\/strong><\/h3>\n\n\n\n<pre id=\"130761e9\" class=\"wp-block-code\"><code>SELECT s1.student_id \nFROM score s1 \nJOIN score s2 ON s1.student_id = s2.student_id \nWHERE s1.course_id = (SELECT cid FROM course WHERE cname = '\u7269\u7406') \n  AND s2.course_id = (SELECT cid FROM course WHERE cname = '\u751f\u7269') \n  AND s1.num &gt; s2.num;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"gVzMj\">9. <strong>\u67e5\u8be2\u6ca1\u6709\u540c\u65f6\u9009\u4fee\u7269\u7406\u8bfe\u7a0b\u548c\u4f53\u80b2\u8bfe\u7a0b\u7684\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"277fc9b9\" class=\"wp-block-code\"><code>SELECT sname \nFROM student \nWHERE sid NOT IN (\n    SELECT student_id \n    FROM score \n    WHERE course_id IN (\n        SELECT cid \n        FROM course \n        WHERE cname IN ('\u7269\u7406', '\u4f53\u80b2')\n    ) \n    GROUP BY student_id \n    HAVING COUNT(DISTINCT course_id) = 2\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"YDRdH\">10. <strong>\u67e5\u8be2\u6302\u79d1\u8d85\u8fc7\u4e24\u95e8(\u5305\u62ec\u4e24\u95e8)\u7684\u5b66\u751f\u59d3\u540d\u548c\u73ed\u7ea7<\/strong><\/h3>\n\n\n\n<pre id=\"654985f6\" class=\"wp-block-code\"><code>SELECT student.sname, class.caption \nFROM student \nJOIN class ON student.class_id = class.cid \nWHERE student.sid IN (\n    SELECT student_id \n    FROM score \n    WHERE num &lt; 60 \n    GROUP BY student_id \n    HAVING COUNT(*) &gt;= 2\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"z954H\">11. <strong>\u67e5\u8be2\u9009\u4fee\u4e86\u6240\u6709\u8bfe\u7a0b\u7684\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"248a24e3\" class=\"wp-block-code\"><code>SELECT sname \nFROM student \nWHERE sid IN (\n    SELECT student_id \n    FROM score \n    GROUP BY student_id \n    HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM course)\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"n6Mbi\">12. <strong>\u67e5\u8be2\u674e\u5e73\u8001\u5e08\u6559\u7684\u8bfe\u7a0b\u7684\u6240\u6709\u6210\u7ee9\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<pre id=\"47e1ad5c\" class=\"wp-block-code\"><code>SELECT * \nFROM score \nWHERE course_id IN (\n    SELECT cid \n    FROM course \n    WHERE teacher_id = (SELECT tid FROM teacher WHERE tname = '\u674e\u5e73\u8001\u5e08')\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"URqu4\">13. <strong>\u67e5\u8be2\u5168\u90e8\u5b66\u751f\u90fd\u9009\u4fee\u4e86\u7684\u8bfe\u7a0b\u53f7\u548c\u8bfe\u7a0b\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"45f910b4\" class=\"wp-block-code\"><code>SELECT course.cid, course.cname \nFROM course \nWHERE NOT EXISTS (\n    SELECT * \n    FROM student \n    WHERE NOT EXISTS (\n        SELECT * \n        FROM score \n        WHERE course.cid = score.course_id \n          AND student.sid = score.student_id\n    )\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"zGnGR\">14. <strong>\u67e5\u8be2\u6bcf\u95e8\u8bfe\u7a0b\u88ab\u9009\u4fee\u7684\u6b21\u6570<\/strong><\/h3>\n\n\n\n<pre id=\"b348c0f0\" class=\"wp-block-code\"><code>SELECT course.cname, COUNT(score.course_id) as selection_count \nFROM course \nLEFT JOIN score ON course.cid = score.course_id \nGROUP BY course.cid;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ZYdSc\">15. <strong>\u67e5\u8be2\u53ea\u9009\u4fee\u4e86\u4e00\u95e8\u8bfe\u7a0b\u7684\u5b66\u751f\u59d3\u540d\u548c\u5b66\u53f7<\/strong><\/h3>\n\n\n\n<pre id=\"2ccc92e8\" class=\"wp-block-code\"><code>SELECT student.sid, student.sname \nFROM student \nWHERE sid IN (\n    SELECT student_id \n    FROM score \n    GROUP BY student_id \n    HAVING COUNT(course_id) = 1\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aCziN\">16. <strong>\u67e5\u8be2\u6240\u6709\u5b66\u751f\u8003\u51fa\u7684\u6210\u7ee9\u5e76\u6309\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\uff08\u6210\u7ee9\u53bb\u91cd\uff09<\/strong><\/h3>\n\n\n\n<pre id=\"b06aaf45\" class=\"wp-block-code\"><code>SELECT DISTINCT num \nFROM score \nORDER BY num DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"G4OAf\">17. <strong>\u67e5\u8be2\u5e73\u5747\u6210\u7ee9\u5927\u4e8e85\u7684\u5b66\u751f\u59d3\u540d\u548c\u5e73\u5747\u6210\u7ee9<\/strong><\/h3>\n\n\n\n<pre id=\"8db9f20e\" class=\"wp-block-code\"><code>SELECT student.sname, AVG(score.num) as avg_score \nFROM score \nJOIN student ON score.student_id = student.sid \nGROUP BY student.sid \nHAVING avg_score &gt; 85;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Xl5NS\">18. <strong>\u67e5\u8be2\u751f\u7269\u6210\u7ee9\u4e0d\u53ca\u683c\u7684\u5b66\u751f\u59d3\u540d\u548c\u5bf9\u5e94\u751f\u7269\u5206\u6570<\/strong><\/h3>\n\n\n\n<pre id=\"54870f30\" class=\"wp-block-code\"><code>SELECT student.sname, score.num \nFROM score \nJOIN student ON score.student_id = student.sid \nWHERE score.course_id = (SELECT cid FROM course WHERE cname = '\u751f\u7269') \n  AND score.num &lt; 60;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"sWYQz\">19. <strong>\u67e5\u8be2\u5728\u6240\u6709\u9009\u4fee\u4e86\u674e\u5e73\u8001\u5e08\u8bfe\u7a0b\u7684\u5b66\u751f\u4e2d\uff0c\u8fd9\u4e9b\u8bfe\u7a0b(\u674e\u5e73\u8001\u5e08\u7684\u8bfe\u7a0b\uff0c\u4e0d\u662f\u6240\u6709\u8bfe\u7a0b)\u5e73\u5747\u6210\u7ee9\u6700\u9ad8\u7684\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"a0ef3134\" class=\"wp-block-code\"><code>SELECT student.sname \nFROM student \nWHERE sid IN (\n    SELECT student_id \n    FROM score \n    WHERE course_id IN (\n        SELECT cid \n        FROM course \n        WHERE teacher_id = (SELECT tid FROM teacher WHERE tname = '\u674e\u5e73\u8001\u5e08')\n    ) \n    GROUP BY student_id \n    ORDER BY AVG(num) DESC \n    LIMIT 1\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"TL12y\">20. <strong>\u67e5\u8be2\u6bcf\u95e8\u8bfe\u7a0b\u6210\u7ee9\u6700\u597d\u7684\u524d\u4e24\u540d\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"ecc2d4f0\" class=\"wp-block-code\"><code>SELECT sname \nFROM (\n    SELECT student.sname, score.course_id, score.num, \n           ROW_NUMBER() OVER(PARTITION BY score.course_id ORDER BY score.num DESC) as rank\n    FROM score \n    JOIN student ON score.student_id = student.sid\n) as ranked \nWHERE rank &lt;= 2;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Hx1IE\">21. <strong>\u67e5\u8be2\u4e0d\u540c\u8bfe\u7a0b\u4f46\u6210\u7ee9\u76f8\u540c\u7684\u5b66\u53f7\uff0c\u8bfe\u7a0b\u53f7\uff0c\u6210\u7ee9<\/strong><\/h3>\n\n\n\n<pre id=\"918c44d8\" class=\"wp-block-code\"><code>SELECT s1.student_id, s1.course_id, s1.num \nFROM score s1 \nJOIN score s2 ON s1.num = s2.num \n              AND s1.course_id != s2.course_id \n              AND s1.student_id = s2.student_id;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"e2Oc6\">22. <strong>\u67e5\u8be2\u6ca1\u5b66\u8fc7\u201c\u53f6\u5e73\u201d\u8001\u5e08\u8bfe\u7a0b\u7684\u5b66\u751f\u59d3\u540d\u4ee5\u53ca\u9009\u4fee\u7684\u8bfe\u7a0b\u540d\u79f0<\/strong><\/h3>\n\n\n\n<pre id=\"f666f569\" class=\"wp-block-code\"><code>SELECT sname, cname \nFROM student \nJOIN score ON student.sid = score.student_id \nJOIN course ON score.course_id = course.cid \nWHERE student.sid NOT IN (\n    SELECT student_id \n    FROM score \n    WHERE course_id IN (\n        SELECT cid \n        FROM course \n        WHERE teacher_id = (SELECT tid FROM teacher WHERE tname = '\u53f6\u5e73')\n    )\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"pVyyE\">23. <strong>\u67e5\u8be2\u6240\u6709\u9009\u4fee\u4e86\u5b66\u53f7\u4e3a1\u7684\u540c\u5b66\u9009\u4fee\u8fc7\u7684\u4e00\u95e8\u6216\u8005\u591a\u95e8\u8bfe\u7a0b\u7684\u540c\u5b66\u5b66\u53f7\u548c\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"5686aa4e\" class=\"wp-block-code\"><code>SELECT DISTINCT s2.sid, s2.sname \nFROM score s1 \nJOIN score s2 ON s1.course_id = s2.course_id \nJOIN student s3 ON s2.student_id = s3.sid \nWHERE s1.student_id = 1 AND s2.student_id != 1;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Uji4D\">24. <strong>\u4efb\u8bfe\u6700\u591a\u7684\u8001\u5e08\u4e2d\u5b66\u751f\u5355\u79d1\u6210\u7ee9\u6700\u9ad8\u7684\u5b66\u751f\u59d3\u540d<\/strong><\/h3>\n\n\n\n<pre id=\"9ecc42a9\" class=\"wp-block-code\"><code>SELECT sname \nFROM student \nWHERE sid IN (\n    SELECT student_id \n    FROM score \n    WHERE course_id IN (\n        SELECT cid \n        FROM course \n        WHERE teacher_id = (\n            SELECT teacher_id \n            FROM (\n                SELECT teacher_id, COUNT(*) as course_count \n                FROM course \n                GROUP BY teacher_id \n                ORDER BY course_count DESC \n                LIMIT 1\n            ) as most_courses\n        )\n    ) \n    ORDER BY num DESC \n    LIMIT 1\n);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7efc\u5408\u7ec3\u4e60\uff1a\u8868\u7ed3\u6784\u4e0e\u6570\u636e\u5bfc\u5165 init.sql \u6587\u4ef6\u5185\u5bb9 \u5bfc\u5165\u6570\u636e \u7ec3\u4e60 1. \u67e5\u8be2\u6240\u6709\u7684\u8bfe\u7a0b\u7684\u540d\u79f0\u4ee5\u53ca\u5bf9\u5e94\u7684\u4efb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12],"tags":[10],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-mysql","category-sql","tag-mysql_lab"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":2,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions\/219"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}