创建表和关系

/*
创建表
*/ /*年级表*/
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)

  

创建数据与查询

/*1、自行创建测试数据;*/
/*
初始化数据
*/ BEGIN;
INSERT INTO `class_grade` VALUES ('', '一年级'), ('', '二年级'), ('', '三年级');
COMMIT; BEGIN;
INSERT INTO `class` VALUES ('', '一年一班',''), ('', '二年一班',''), ('', '三年二班',''), ('', '二年二班','');
COMMIT; BEGIN;
INSERT INTO `teacher` VALUES ('', '张三'), ('', '李四'), ('', '王五'), ('', '朱云海'), ('', '李杰');
COMMIT; BEGIN;
INSERT INTO `student` VALUES ('', '女', '', '乔丹'), ('', '女', '', '艾弗森'), ('', '男', '', '科比'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四');
COMMIT; BEGIN;
INSERT INTO `course` VALUES ('', '生物', ''), ('', '体育', ''),('', '物理', ''), ('', '美术', ''),('', '语文', ''),('', '数学', ''),('', '英语', ''),('', '化学', '');
COMMIT; BEGIN;
INSERT INTO `teach2cls` VALUES ('', '',''), ('', '',''), ('', '',''), ('', '',''),('', '','');
COMMIT; BEGIN;
INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''),
('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
COMMIT; /*2、查询学生总人数;*/
select count(sid) as '学生总人数' from student /*3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;*/
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 ('生物' , '物理')) /*4、查询每个年级的班级数,取出班级数最多的前三个年级;*/
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; /*5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;*/
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 /*6、查询每个年级的学生人数;*/
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; /*7、查询每位学生的学号,姓名,选课数,平均成绩;*/
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 /*8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;*/
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 = ''
ORDER BY score DESC
LIMIT 1) t1 UNION (SELECT
student_id, course_id, score
FROM
score
WHERE
student_id = ''
ORDER BY score
LIMIT 1)) t2
INNER JOIN
student ON sid = t2.student_id
INNER JOIN
course ON cid = t2.course_id /*9、查询姓“李”的老师的个数和所带班级数;*/
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 /*10、查询班级数小于5的年级id和年级名;*/
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; /* 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果
如下;
*/
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 ('' , '') THEN '低年级'
WHEN t.gid IN ('' , '') THEN '中年级'
WHEN t.gid IN ('' , '') THEN '高年级'
END) gid_level
FROM
class_grade t) AS t1 ON t1.gid = class.grade_id /*12、查询学过“张三”老师2门课以上的同学的学号、姓名;*/
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) /*13、查询教授课程超过2门的老师的id和姓名;*/
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) /* 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;*/
SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
course_id IN ('' , '')) /*15、查询没有带过高年级的老师id和姓名;*/
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 ('' , '')))) /*16、查询学过“张三”老师所教的所有课的同学的学号、姓名;*/
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 = '张三'))) /*17、查询带过超过2个班级的老师的id和姓名;*/
SELECT
tid '老师id', tname '姓名'
FROM
teacher
WHERE
tid IN (SELECT
tid
FROM
teach2cls
GROUP BY tid
HAVING COUNT(tid) > 2) /*18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;*/ 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 = '') t1
INNER JOIN
(SELECT
student_id, score score_2
FROM
score
WHERE
course_id = '') t2 ON t1.student_id = t2.student_id
WHERE
t2.score_2 < t1.score_1) /*19、查询所带班级数最多的老师id和姓名;*/
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)) /*20、查询有课程成绩小于60分的同学的学号、姓名;*/
SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
score < 60) /*21、查询没有学全所有课的同学的学号、姓名;*/
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)) /*22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;*/
SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
student_id <> ''
AND course_id IN (SELECT
course_id
FROM
score
WHERE
student_id = '')) /*23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;*/
SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
student_id <> ''
AND course_id IN (SELECT
course_id
FROM
score
WHERE
student_id = '')) /*24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;*/ SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
student_id <> ''
AND course_id IN (SELECT
course_id
FROM
score
WHERE
student_id = '')
GROUP BY student_id
HAVING COUNT(student_id) = (SELECT
COUNT(course_id)
FROM
score
WHERE
student_id = '')) /*25、删除学习“张三”老师课的score表记录;*/
DELETE FROM score
WHERE
course_id IN (SELECT
cid
FROM
course WHERE
teacher_id = (SELECT
tid
FROM
teacher WHERE
tname = '张三')); /*26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课
程的平均成绩;
*/ ①没有上过编号“2”课程的同学学号
SELECT
student.sid
FROM
student
WHERE
student.sid not in(
SELECT
student_id
FROM
score
WHERE
course_id in ('')) ②插入“2”号课程的平均成绩; SELECT
AVG(score)
FROM
score
WHERE
course_id = '' INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''),('', '', '', '');
commit; /*27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,
数学,英语,有效课程数,有效平均分;*/
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) /*28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;*/
SELECT
course_id AS '课程ID',
MAX(score) AS '最高分',
MIN(score) AS '最低分'
FROM
score
GROUP BY course_id; /*29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;*/ 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; /*30、课程平均分从高到低显示(显示任课老师);*/ 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; /*31、查询各科成绩前三名的记录(不考虑成绩并列情况) ;
*/ 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 /*32、查询每门课程被选修的学生数; */
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 /*33、查询选修了2门以上课程的全部学生的学号和姓名;*/
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) /*34、查询男生、女生的人数,按倒序排列;*/
SELECT
gender '性别', COUNT(sid) '人数'
FROM
student
GROUP BY gender
ORDER BY COUNT(sid) DESC /*35、查询姓“张”的学生名单;*/ SELECT
sid '学号', sname '姓名'
FROM
student
WHERE
sname LIKE '张%' /*36、查询同名同姓学生名单,并统计同名人数;
*/
SELECT
sname '学生名', COUNT(sname) '人数'
FROM
student
GROUP BY sname
HAVING COUNT(sname) > 1 /*37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
*/
SELECT
course_id '课程号' , AVG(score) '平均成绩'
FROM
score
GROUP BY course_id
ORDER BY AVG(score) ASC , course_id DESC /*38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;*/
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 /*39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;*/
SELECT
student.sid '学号', student.sname '姓名'
FROM
student
WHERE
student.sid IN (SELECT
student_id
FROM
score
WHERE
course_id = '' AND score > 80) /*40、求选修了课程的学生人数;*/
SELECT
COUNT(DISTINCT student_id) '学生人数'
FROM
score /*41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
*/
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 /*42、查询各个课程及相应的选修人数;*/
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 /*43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;*/
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 /*44、查询每门课程成绩最好的前两名学生id和姓名;*/ 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) /*45、检索至少选修两门课程的学生学号;*/
SELECT
student_id
FROM
score
GROUP BY student_id
HAVING COUNT(course_id) >= 2; /*46、查询没有学生选修的课程的课程号和课程名;*/
SELECT
cid '课程号', cname '课程名'
FROM
course
WHERE
cid NOT IN (SELECT DISTINCT
course_id
FROM
score) /*47、查询没带过任何班级的老师id和姓名;*/
SELECT
tid '老师id', tname '姓名'
FROM
teacher
WHERE
tid NOT IN (SELECT
tid
FROM
teach2cls) /*48、查询有两门以上课程超过80分的学生id及其平均成绩*/
SELECT
student_id '学号', AVG(score) '平均成绩'
FROM
score
WHERE
score > 80
GROUP BY student_id
HAVING COUNT(course_id) > 2 /*49、检索“3”课程分数小于60,按分数降序排列的同学学号;*/
SELECT
student_id '学号'
FROM
score
WHERE
course_id = '' AND score < 60
ORDER BY score DESC; /*50、删除编号为“2”的同学的“1”课程的成绩;*/ delete from score where student_id='' and course_id=''; /*51、查询同时选修了物理课和生物课的学生id和姓名。*/
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)

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. 2019.01.17 bzoj1853: [Scoi2010]幸运数字(容斥+dfs)

    传送门 搜索菜题,然而第一次没有注意然后爆longlonglong longlonglong了. 题意:称所有数位由6,86,86,8组成的数为幸运数字,问一个一个区间[l,r][l,r][l,r]中 ...

  2. java Concurrent包学习笔记(七):ConcurrentHashMap

    (注意:以下讲解的ConcurrentHashMap是jdk 1.8的) 一.ConcurrentHashMap的数据结构 ConcurrentHashMap在1.8中的实现,相比于1.7的版本基本上 ...

  3. hadoop sqoop的常用名命令

    1 列出所有的ambari数据库中所有的表 Sqoop list-tables -connect  jdbc:mysql://localhost:3306/ambari -username ambar ...

  4. 微信小程序 js结构

    // pages/index/index.js Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad: function ...

  5. 20155205 2016-2017-2 《Java程序设计》第6周学习总结

    20155205 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 在Java中,输入串流代表对象为Java.io.InputStream实例,输出串流 ...

  6. 最完美的Linux桌面软件

    下面是关于Linux桌面环境下,目前为止最完美的部分.之所以说他们完美,是因为他们不但很养眼,而且也使用最好的多媒体技术,有最好的可用性.在某些方面,他们甚至超过了Windows和Mac-OS. 基础 ...

  7. (转)自己来控制EntityFramework4.1 Code-First,逐步消除EF之怪异现象

    转自:http://www.cnblogs.com/richwong/archive/2011/07/06/2098759.html 最近的项目开始使用EF4.1,拜读各路大侠文章数遍,满以为可以轻车 ...

  8. MapGIS SDK(C++)【基础篇】

    算法测试:Demo.Test https://www.cnblogs.com/2008nmj/p/10060847.html //例1-1 简单要素 void AppendSFeature(CSFea ...

  9. C++插件架构浅谈与初步实现

    一.插件架构初步介绍 想到写本博客,也没想到更好的名字,目前就先命这个名吧.说到插件架构,或许大部分IT从业者都听过或者某些牛人也自己实现过稳定高效的插件框架.目前有很多软件以及库都是基于插件架构,例 ...

  10. Hdu3829 Cat VS Dog(最大独立点集)

    Cat VS Dog Problem Description The zoo have N cats and M dogs, today there are P children visiting t ...