创建表和关系

-
-
-
- DROP TABLE IF EXISTS `class_grade`;
- CREATE TABLE `class_grade` (
- `gid` int(11) NOT NULL AUTO_INCREMENT,
- `gname` varchar(32) NOT NULL,
- PRIMARY KEY (`gid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
-
- DROP TABLE IF EXISTS `class`;
- CREATE TABLE `class` (
- `cid` int(11) NOT NULL AUTO_INCREMENT,
- `caption` varchar(32) NOT NULL,
- `grade_id` int(11) NOT NULL,
- PRIMARY KEY (`cid`),
- KEY `fk_class_grade` (`grade_id`),
- CONSTRAINT `fk_class_grade` FOREIGN KEY (`grade_id`) REFERENCES `class_grade` (`gid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
- DROP TABLE IF EXISTS `teacher`;
- CREATE TABLE `teacher` (
- `tid` int(11) NOT NULL AUTO_INCREMENT,
- `tname` varchar(32) NOT NULL,
- PRIMARY KEY (`tid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
-
-
- DROP TABLE IF EXISTS `course`;
- CREATE TABLE `course` (
- `cid` int(11) NOT NULL AUTO_INCREMENT,
- `cname` varchar(32) NOT NULL,
- `teacher_id` int(11) NOT NULL,
- PRIMARY KEY (`cid`),
- KEY `fk_course_teacher` (`teacher_id`),
- CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
- DROP TABLE IF EXISTS `student`;
- CREATE TABLE `student` (
- `sid` int(11) NOT NULL AUTO_INCREMENT,
- `gender` char(1) NOT NULL,
- `class_id` int(11) NOT NULL,
- `sname` varchar(32) NOT NULL,
- PRIMARY KEY (`sid`),
- KEY `fk_class` (`class_id`),
- CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
-
- DROP TABLE IF EXISTS `score`;
- CREATE TABLE `score` (
- `sid` int(11) NOT NULL AUTO_INCREMENT,
- `student_id` int(11) NOT NULL,
- `course_id` int(11) NOT NULL,
- `score` int(11) NOT NULL,
- PRIMARY KEY (`sid`),
- KEY `fk_score_student` (`student_id`),
- KEY `fk_score_course` (`course_id`),
- CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
- CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
-
-
- DROP TABLE IF EXISTS `teach2cls`;
- CREATE TABLE `teach2cls`(
- `tcid` int(11) NOT NULL AUTO_INCREMENT,
- `tid` int(11) NOT NULL,
- `cid` int(11) NOT NULL,
- PRIMARY KEY (`tcid`),
- KEY `fk_teach2cls_class` (`cid`),
- KEY `fk_teach2cls_teacher` (`tid`),
- CONSTRAINT `fk_teach2cls_class` FOREIGN KEY (`cid`) REFERENCES `class` (`cid`),
- CONSTRAINT `fk_teach2cls_teacher` FOREIGN KEY (`tid`) REFERENCES `teacher` (`tid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
-
-
-
-
- mysql> use db4;
- Database changed
- mysql> desc class;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | cid | int(11) | NO | PRI | NULL | auto_increment |
- | caption | varchar(32) | NO | | NULL | |
- | grade_id | int(11) | NO | MUL | NULL | |
- +
- 3 rows in set (0.01 sec)
-
- mysql> select * from class;
- +
- | cid | caption | grade_id |
- +
- | 1 | 一年一班 | 1 |
- | 2 | 二年一班 | 2 |
- | 3 | 三年二班 | 3 |
- | 4 | 二年二班 | 2 |
- +
- 4 rows in set (0.00 sec)
-
- mysql>
- mysql> desc student;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | sid | int(11) | NO | PRI | NULL | auto_increment |
- | gender | char(1) | NO | | NULL | |
- | class_id | int(11) | NO | MUL | NULL | |
- | sname | varchar(32) | NO | | NULL | |
- +
- 4 rows in set (0.01 sec)
-
- mysql> select * from student limit 5;
- +
- | sid | gender | class_id | sname |
- +
- | 1 | 女 | 1 | 乔丹 |
- | 2 | 女 | 1 | 艾弗森 |
- | 3 | 男 | 2 | 科比 |
- | 4 | 男 | 1 | 张一 |
- | 5 | 女 | 1 | 张二 |
- +
- 5 rows in set (0.00 sec)
-
- mysql> desc teacher;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | tid | int(11) | NO | PRI | NULL | auto_increment |
- | tname | varchar(32) | NO | | NULL | |
- +
- 2 rows in set (0.01 sec)
-
- mysql> select * from teacher;
- +
- | tid | tname |
- +
- | 1 | 张三 |
- | 2 | 李四 |
- | 3 | 王五 |
- | 4 | 朱云海 |
- | 5 | 李杰 |
- +
- 5 rows in set (0.00 sec)
-
- mysql> desc course;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | cid | int(11) | NO | PRI | NULL | auto_increment |
- | cname | varchar(32) | NO | | NULL | |
- | teacher_id | int(11) | NO | MUL | NULL | |
- +
- 3 rows in set (0.01 sec)
-
- mysql> select * from course;
- +
- | cid | cname | teacher_id |
- +
- | 1 | 生物 | 1 |
- | 2 | 体育 | 1 |
- | 3 | 物理 | 2 |
- | 4 | 美术 | 2 |
- +
- 4 rows in set (0.00 sec)
-
- mysql> desc score;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | sid | int(11) | NO | PRI | NULL | auto_increment |
- | student_id | int(11) | NO | MUL | NULL | |
- | course_id | int(11) | NO | MUL | NULL | |
- | score | int(11) | NO | | NULL | |
- +
- 4 rows in set (0.01 sec)
-
- mysql> select * from score limit 5;
- +
- | sid | student_id | course_id | score |
- +
- | 1 | 1 | 1 | 60 |
- | 2 | 1 | 2 | 59 |
- | 3 | 2 | 2 | 99 |
- | 6 | 2 | 1 | 8 |
- | 8 | 2 | 3 | 68 |
- +
- 5 rows in set (0.00 sec)
-
- mysql> desc class_grade;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | gid | int(11) | NO | PRI | NULL | auto_increment |
- | gname | varchar(32) | NO | | NULL | |
- +
- 2 rows in set (0.01 sec)
-
- mysql> select * from class_grade;
- +
- | gid | gname |
- +
- | 1 | 一年级 |
- | 2 | 二年级 |
- | 3 | 三年级 |
- +
- 3 rows in set (0.00 sec)
-
- mysql> desc teach2cls;
- +
- | Field | Type | Null | Key | Default | Extra |
- +
- | tcid | int(11) | NO | PRI | NULL | auto_increment |
- | tid | int(11) | NO | MUL | NULL | |
- | cid | int(11) | NO | MUL | NULL | |
- +
- 3 rows in set (0.01 sec)
-
- mysql> select * from teach2cls;
- +
- | tcid | tid | cid |
- +
- | 1 | 1 | 1 |
- | 2 | 1 | 2 |
- | 3 | 2 | 1 |
- | 4 | 3 | 2 |
- +
- 4 rows in set (0.00 sec)
-
创建数据与查询
-
-
-
- BEGIN;
- INSERT INTO `class_grade` VALUES ('1', '一年级'), ('2', '二年级'), ('3', '三年级');
- COMMIT;
-
- BEGIN;
- INSERT INTO `class` VALUES ('1', '一年一班','1'), ('2', '二年一班','2'), ('3', '三年二班','3'), ('4', '二年二班','2');
- COMMIT;
-
- BEGIN;
- INSERT INTO `teacher` VALUES ('1', '张三'), ('2', '李四'), ('3', '王五'), ('4', '朱云海'), ('5', '李杰');
- COMMIT;
-
- BEGIN;
- INSERT INTO `student` VALUES ('1', '女', '1', '乔丹'), ('2', '女', '1', '艾弗森'), ('3', '男', '2', '科比'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四');
- COMMIT;
-
-
- BEGIN;
- INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '体育', '1'),('3', '物理', '2'), ('4', '美术', '2'),('5', '语文', '4'),('6', '数学', '5'),('7', '英语', '3'),('8', '化学', '3');
- COMMIT;
-
-
- BEGIN;
- INSERT INTO `teach2cls` VALUES ('1', '1','1'), ('2', '1','2'), ('3', '2','1'), ('4', '3','2'),('5', '3','4');
- COMMIT;
-
-
- BEGIN;
- INSERT INTO `score` VALUES ('1', '1', '1', '60'), ('2', '1', '2', '59'), ('3', '2', '2', '99'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '95'), ('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'),
- ('41', '1', '5', '60'), ('42', '1', '6', '59'), ('43', '2', '7', '99'), ('44', '3', '5', '60'), ('45', '2', '6', '59'), ('46', '3', '7', '89'), ('48', '1', '7', '95'), ('49', '2', '5', '59'), ('50', '3', '6', '87');
- COMMIT;
-
-
- select count(sid) as '学生总人数' from student
-
-
-
- SELECT
- student.sid as '学生id', student.sname as '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- INNER JOIN
- course ON course.cid = score.course_id
- WHERE
- score >= 60
- AND course.cname IN ('生物' , '物理'))
-
-
-
-
- SELECT
- t1.gid, t1.gname as '年级名称'
- FROM
- (SELECT
- grade_id, COUNT(cid) cid_num
- FROM
- class
- GROUP BY class.grade_id
- ORDER BY cid_num DESC
- LIMIT 3) t2
- INNER JOIN
- class_grade t1 ON t2.grade_id = t1.gid;
-
-
-
- SELECT
- student.sid as '学生id', student.sname as '姓名', avg_score as '平均成绩'
- FROM
- student
- INNER JOIN
- (SELECT
- student_id, avg_score
- FROM
- (SELECT
- student_id, AVG(score) AS avg_score
- FROM
- score
- GROUP BY student_id
- ORDER BY AVG(score) DESC
- LIMIT 1) AS t1 UNION (SELECT
- student_id, AVG(score) AS avg_score
- FROM
- score
- GROUP BY student_id
- ORDER BY AVG(score)
- LIMIT 1)) t2 ON student.sid = t2.student_id
-
-
- SELECT
- gid, COUNT(student.sid) as '学生人数'
- FROM
- class_grade
- LEFT JOIN
- class ON class.grade_id = gid
- LEFT JOIN
- student ON student.class_id = class.cid
- GROUP BY gid;
-
-
-
- SELECT
- student.sid as '学号', student.sname as '姓名', IFNULL(avg_score,0) '平均成绩', IFNULL(course_num,0) '选课数'
- FROM
- student
- LEFT JOIN
- (SELECT
- student_id,
- AVG(score) avg_score,
- COUNT(course_id) course_num
- FROM
- score
- GROUP BY student_id) AS t1 ON student.sid = t1.student_id
-
-
-
- SELECT
- student.sname as '学生姓名', course.cname as '课程名', t2.score as '分数'
- FROM
- (SELECT
- student_id, course_id, score
- FROM
- (SELECT
- student_id, course_id, score
- FROM
- score
- WHERE
- student_id = '2'
- ORDER BY score DESC
- LIMIT 1) t1 UNION (SELECT
- student_id, course_id, score
- FROM
- score
- WHERE
- student_id = '2'
- ORDER BY score
- LIMIT 1)) t2
- INNER JOIN
- student ON sid = t2.student_id
- INNER JOIN
- course ON cid = t2.course_id
-
-
-
-
- SELECT
- t1.tid, tid_num as '个数', IFNULL(cid_num, 0) as '班级数'
- FROM
- (SELECT
- tid, COUNT(tid) tid_num
- FROM
- teacher
- WHERE
- tname LIKE '李%'
- GROUP BY tid) t1
- LEFT JOIN
- (SELECT
- tid, COUNT(cid) cid_num
- FROM
- teach2cls
- GROUP BY tid) t2 ON t2.tid = t1.tid
-
-
-
- SELECT
- t1.gid as '年级id' , t1.gname as '年级名'
- FROM
- (SELECT
- grade_id, COUNT(cid) cid_num
- FROM
- class
- GROUP BY class.grade_id
- HAVING COUNT(cid) < 5) t2
- INNER JOIN
- class_grade t1 ON t2.grade_id = t1.gid;
-
-
- SELECT
- cid as '班级id', caption as '班级名称', t1.gname as '年级' , t1.gid_level as '年级级别'
- FROM
- class
- INNER JOIN
- (SELECT
- t.gid,
- t.gname,
- (CASE
- WHEN t.gid IN ('1' , '2') THEN '低年级'
- WHEN t.gid IN ('3' , '4') THEN '中年级'
- WHEN t.gid IN ('5' , '6') THEN '高年级'
- END) gid_level
- FROM
- class_grade t) AS t1 ON t1.gid = class.grade_id
-
-
-
-
- SELECT
- sid '学号', sname '姓名'
- FROM
- student
- WHERE
- sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- course_id IN (SELECT
- cid
- FROM
- course
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
- WHERE
- tname = '张三'))
- GROUP BY student_id
- HAVING COUNT(student_id) >= 2)
-
-
-
-
- SELECT
- tid as '老师id', tname as '老师姓名'
- FROM
- teacher
- WHERE
- tid IN (SELECT
- teacher_id
- FROM
- course
- GROUP BY teacher_id
- HAVING COUNT(teacher_id) > 2)
-
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- course_id IN ('1' , '2'))
-
-
-
-
- SELECT
- tid '老师id', tname '姓名'
- FROM
- teacher
- WHERE
- tid IN (SELECT
- tid
- FROM
- teach2cls
- WHERE
- cid IN (SELECT
- cid
- FROM
- class
- WHERE
- grade_id IN (SELECT
- t.gid
- FROM
- class_grade t
- WHERE
- t.gid NOT IN ('5' , '6'))))
-
-
-
-
-
- SELECT
- sid '学号', sname '姓名'
- FROM
- student
- WHERE
- sid IN (SELECT
- student_id
- FROM
- score
- INNER JOIN
- (SELECT
- cid
- FROM
- course
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
- WHERE
- tname = '张三')) t1 ON t1.cid = score.course_id
- GROUP BY student_id
- HAVING COUNT(student_id) = (SELECT
- COUNT(cid)
- FROM
- course
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
- WHERE
- tname = '张三')))
-
-
-
-
- SELECT
- tid '老师id', tname '姓名'
- FROM
- teacher
- WHERE
- tid IN (SELECT
- tid
- FROM
- teach2cls
- GROUP BY tid
- HAVING COUNT(tid) > 2)
-
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- t2.student_id
- FROM
- (SELECT
- student_id, score score_1
- FROM
- score
- WHERE
- course_id = '1') t1
- INNER JOIN
- (SELECT
- student_id, score score_2
- FROM
- score
- WHERE
- course_id = '2') t2 ON t1.student_id = t2.student_id
- WHERE
- t2.score_2 < t1.score_1)
-
-
-
-
- SELECT
- tid '老师id', tname '姓名'
- FROM
- teacher
- WHERE
- tid IN (SELECT
- tid
- FROM
- teach2cls
- GROUP BY tid
- HAVING COUNT(tid) = (SELECT
- MAX(t1.tid_num)
- FROM
- (SELECT
- tid, COUNT(tid) tid_num
- FROM
- teach2cls
- GROUP BY tid) t1))
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- score < 60)
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid NOT IN (SELECT
- student_id
- FROM
- score
- GROUP BY student_id
- HAVING COUNT(student_id) = (SELECT
- COUNT(cid)
- FROM
- course))
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- student_id <> '1'
- AND course_id IN (SELECT
- course_id
- FROM
- score
- WHERE
- student_id = '1'))
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- student_id <> '1'
- AND course_id IN (SELECT
- course_id
- FROM
- score
- WHERE
- student_id = '1'))
-
-
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- student_id <> '2'
- AND course_id IN (SELECT
- course_id
- FROM
- score
- WHERE
- student_id = '2')
- GROUP BY student_id
- HAVING COUNT(student_id) = (SELECT
- COUNT(course_id)
- FROM
- score
- WHERE
- student_id = '2'))
-
-
-
- DELETE FROM score
- WHERE
- course_id IN (SELECT
- cid
- FROM
- course
-
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
-
- WHERE
- tname = '张三'));
-
-
-
-
-
- ①没有上过编号“2”课程的同学学号
- SELECT
- student.sid
- FROM
- student
- WHERE
- student.sid not in(
- SELECT
- student_id
- FROM
- score
- WHERE
- course_id in ('2'))
-
- ②插入“2”号课程的平均成绩;
-
- SELECT
- AVG(score)
- FROM
- score
- WHERE
- course_id = '2'
-
-
- INSERT INTO `score` VALUES ('35', '7', '1', '57'), ('36', '8', '2', '57'),('37', '9', '3', '57');
- commit;
-
-
-
-
- SELECT
- student_id AS '学生ID',
- IFNULL(AVG(CASE
- WHEN course.cname = '语文' THEN score
- END),
- 0) AS '语文',
- IFNULL(AVG(CASE
- WHEN course.cname = '数学' THEN score
- END),
- 0) AS '数学',
- IFNULL(AVG(CASE
- WHEN course.cname = '英语' THEN score
- END),
- 0) AS '英语',
- COUNT(student_id) AS '有效课程数',
- AVG(score) AS '有效平均分'
- FROM
- score
- INNER JOIN
- course ON course.cid = score.course_id
- GROUP BY student_id
- ORDER BY AVG(score)
-
-
-
- SELECT
- course_id AS '课程ID',
- MAX(score) AS '最高分',
- MIN(score) AS '最低分'
- FROM
- score
- GROUP BY course_id;
-
-
-
-
- SELECT
- course_id AS '课程ID',
- AVG(score) AS '平均成绩',
- SUM(CASE
- WHEN score >= 60 THEN 1
- ELSE 0
- END) / COUNT(course_id) * 100 AS '及格率'
- FROM
- score
- GROUP BY course_id
- ORDER BY AVG(score) ASC , SUM(CASE
- WHEN score >= 60 THEN 1
- ELSE 0
- END) / COUNT(course_id) * 100 DESC;
-
-
-
-
- SELECT
- course_id AS '课程ID',
- AVG(score) AS '平均成绩',
- teacher.tname
- FROM
- score
- INNER JOIN
- course ON course_id = course.cid
- INNER JOIN
- teacher ON teacher.tid = course.teacher_id
- GROUP BY course_id
- ORDER BY AVG(score) DESC;
-
-
-
-
- SELECT
- course_id, MAX(score) score
- FROM
- score
- GROUP BY course_id
- UNION (SELECT
- score.course_id, MAX(score) AS score
- FROM
- score
- INNER JOIN
- (SELECT
- course_id, MAX(score) first_num
- FROM
- score
- GROUP BY course_id) t1 ON score.course_id = t1.course_id
- WHERE
- score.score < t1.first_num
- GROUP BY score.course_id)
- UNION (SELECT
- score.course_id, MAX(score) AS score
- FROM
- score
- INNER JOIN
- (SELECT
- score.course_id, MAX(score) AS second_num
- FROM
- score
- INNER JOIN (SELECT
- course_id, MAX(score) first_num
- FROM
- score
- GROUP BY course_id) t1 ON score.course_id = t1.course_id
- WHERE
- score.score < t1.first_num
- GROUP BY score.course_id) t2 ON score.course_id = t2.course_id
- WHERE
- score.score < t2.second_num
- GROUP BY score.course_id) ORDER BY course_id , score DESC
-
-
-
- SELECT
- cname '课程', IFNULL(stu_num, 0) '学生数'
- FROM
- course
- LEFT JOIN
- (SELECT
- course_id, COUNT(student_id) stu_num
- FROM
- score
- GROUP BY course_id) t1 ON course.cid = t1.course_id
-
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- GROUP BY student_id
- HAVING COUNT(course_id) > 2)
-
-
-
- SELECT
- gender '性别', COUNT(sid) '人数'
- FROM
- student
- GROUP BY gender
- ORDER BY COUNT(sid) DESC
-
-
-
- SELECT
- sid '学号', sname '姓名'
- FROM
- student
- WHERE
- sname LIKE '张%'
-
-
-
- SELECT
- sname '学生名', COUNT(sname) '人数'
- FROM
- student
- GROUP BY sname
- HAVING COUNT(sname) > 1
-
-
- SELECT
- course_id '课程号' , AVG(score) '平均成绩'
- FROM
- score
- GROUP BY course_id
- ORDER BY AVG(score) ASC , course_id DESC
-
-
-
-
- SELECT
- student.sname '学生姓名', score '成绩'
- FROM
- student
- INNER JOIN
- (SELECT DISTINCT
- student_id, score
- FROM
- score
- INNER JOIN course ON course.cid = score.course_id
- WHERE
- score < 60 AND course.cname = '数学') t1 ON student.sid = t1.student_id
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- WHERE
- course_id = '3' AND score > 80)
-
-
-
- SELECT
- COUNT(DISTINCT student_id) '学生人数'
- FROM
- score
-
-
-
- SELECT
- sname '学生姓名', t4.score '成绩'
- FROM
- student
- INNER JOIN
- (SELECT
- student_id, score
- FROM
- (SELECT
- student_id, score
- FROM
- score
- INNER JOIN (SELECT
- cid
- FROM
- course
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
- WHERE
- tname = '王五')) t1 ON t1.cid = score.course_id
- ORDER BY score DESC
- LIMIT 1) t2 UNION (SELECT
- student_id, score
- FROM
- score
- INNER JOIN (SELECT
- cid
- FROM
- course
- WHERE
- teacher_id = (SELECT
- tid
- FROM
- teacher
- WHERE
- tname = '王五')) t1 ON t1.cid = score.course_id
- ORDER BY score ASC
- LIMIT 1)) t4 ON t4.student_id = student.sid
-
-
-
-
- SELECT
- cname '课程', IFNULL(stu_num, 0) '选修人数'
- FROM
- course
- LEFT JOIN
- (SELECT
- t1.course_id, COUNT(t1.student_id) stu_num
- FROM
- score t1
- GROUP BY t1.course_id) t2 ON course.cid = t2.course_id
-
-
-
- SELECT DISTINCT
- t1.student_id '学号', t1.course_id '课程号', t1.score '学生成绩'
- FROM
- score t1
- INNER JOIN
- score ON t1.student_id = score.student_id
- WHERE
- t1.course_id <> score.course_id
- AND t1.score = score.score
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- score.student_id
- FROM
- score
- INNER JOIN
- (SELECT
- t3.course_id, first_num, second_num
- FROM
- (SELECT
- course_id, MAX(score) first_num
- FROM
- score
- GROUP BY course_id) t3
- INNER JOIN (SELECT
- score.course_id, MAX(score) AS second_num
- FROM
- score
- INNER JOIN (SELECT
- course_id, MAX(score) first_num
- FROM
- score
- GROUP BY course_id) t1 ON score.course_id = t1.course_id
- WHERE
- score.score < t1.first_num
- GROUP BY score.course_id) t2 ON t3.course_id = t2.course_id) t4 ON score.course_id = t4.course_id
- WHERE
- score.score <= t4.first_num
- AND score.score >= t4.second_num)
-
-
-
-
- SELECT
- student_id
- FROM
- score
- GROUP BY student_id
- HAVING COUNT(course_id) >= 2;
-
-
-
- SELECT
- cid '课程号', cname '课程名'
- FROM
- course
- WHERE
- cid NOT IN (SELECT DISTINCT
- course_id
- FROM
- score)
-
-
-
- SELECT
- tid '老师id', tname '姓名'
- FROM
- teacher
- WHERE
- tid NOT IN (SELECT
- tid
- FROM
- teach2cls)
-
-
-
- SELECT
- student_id '学号', AVG(score) '平均成绩'
- FROM
- score
- WHERE
- score > 80
- GROUP BY student_id
- HAVING COUNT(course_id) > 2
-
-
- SELECT
- student_id '学号'
- FROM
- score
- WHERE
- course_id = '3' AND score < 60
- ORDER BY score DESC;
-
-
-
-
- delete from score where student_id='2' and course_id='1';
-
-
-
- SELECT
- student.sid '学号', student.sname '姓名'
- FROM
- student
- WHERE
- student.sid IN (SELECT
- student_id
- FROM
- score
- INNER JOIN
- course ON course.cid = score.course_id
- WHERE
- course.cname IN ('生物' , '物理')
- GROUP BY student_id
- HAVING COUNT(course_id) = 2)