-- 1.查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)
mysql> select student.sname,score.number from student left join score on student_id = student.sid where score.number > 60;
+-------+--------+
| sname | number |
+-------+--------+
| 铁锤 | 100 |
+-------+--------+
1 row in set (0.00 sec)
-- 2.查询每个老师教授的课程数量 和 老师信息
mysql> select teacher.tname,count(tearch_id) from teacher right join course on tearch_id = teacher.tid group by tearch_id;
+-------+------------------+
| tname | count(tearch_id) |
+-------+------------------+
| 波多 | 2 |
| 苍空 | 1 |
+-------+------------------+
2 rows in set (0.00 sec)
-- 3. 查询学生的信息以及学生所在的班级信息
mysql> select student.sname, class.caption from student left join class on class_id = class.cid;
+-------+----------+
| sname | caption |
+-------+----------+
| 钢蛋 | 三年二班 |
| 铁锤 | 三年二班 |
| 山炮 | 一年一班 |
+-------+----------+
3 rows in set (0.00 sec)
-- 4、学生中男生的个数和女生的个数
mysql> select gender,count(gender) from student group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男 | 1 |
| 女 | 2 |
+--------+---------------+
2 rows in set (0.00 sec)
-- 5、获取所有学习'生物'的学生的学号和成绩;姓名
mysql> select student.sid,student.sname,score.number from score
-> left join student on score.student_id = student.sid
-> left join course on score.corse_id = course.cid
-> where course.cname = '生物';
+------+-------+--------+
| sid | sname | number |
+------+-------+--------+
| 1 | 钢蛋 | 60 |
+------+-------+--------+
1 row in set (0.00 sec)
-- 6、查询平均成绩大于60分的同学的学号和平均成绩;
mysql> select student.sid,avg(number) from student left join score on student_id = student.sid
-> group by student.sid
-> having avg(number) > 60;
+-----+-------------+
| sid | avg(number) |
+-----+-------------+
| 2 | 100.0000 |
+-----+-------------+
1 row in set (0.00 sec)
-- 7、查询姓“李”的老师的个数;
mysql> select teacher.tname ,count(tname) from teacher group by tname;
+-------+--------------+
| tname | count(tname) |
+-------+--------------+
| 波多 | 1 |
| 苍空 | 1 |
| 饭岛 | 1 |
+-------+--------------+
3 rows in set (0.00 sec)
-- 8、查询课程成绩小于60分的同学的学号、姓名;
mysql> select student.sid,student.sname from student left join score on student_id
= student.sid where score.number < 60;
+-----+-------+
| sid | sname |
+-----+-------+
| 1 | 钢蛋 |
+-----+-------+
1 row in set (0.00 sec)
-- 9. 删除学习“叶平”老师课的SC表记录
-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
mysql> select course.cid,max(number),min(number) from score
-> left join course on score.corse_id = course.cid
-> group by corse_id;
+------+-------------+-------------+
| cid | max(number) | min(number) |
+------+-------------+-------------+
| 1 | 60 | 60 |
| 2 | 100 | 59 |
+------+-------------+-------------+
2 rows in set (0.00 sec)
-- 11.查询每门课程被选修的学生数
mysql> select score.corse_id, count(corse_id) from score group by corse_id;
+----------+-----------------+
| corse_id | count(corse_id) |
+----------+-----------------+
| 1 | 1 |
| 2 | 2 |
+----------+-----------------+
2 rows in set (0.00 sec)
-- 12.查询姓“张”的学生名单;
mysql> select * from student where sname like '山%';
+-----+-------+--------+----------+
| sid | sname | gender | class_id |
+-----+-------+--------+----------+
| 3 | 山炮 | 男 | 2 |
+-----+-------+--------+----------+
1 row in set (0.00 sec)
-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
mysql> select corse_id, avg(number) from score group by corse_id order by avg(number) asc,corse_id desc;
+----------+-------------+
| corse_id | avg(number) |
+----------+-------------+
| 1 | 60.0000 |
| 2 | 79.5000 |
+----------+-------------+
2 rows in set (0.00 sec)
-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩
mysql> select student.sid,student.sname, avg(number) from student left join score on student_id = student.sid group by student.sid having avg(number) > 85;
+-----+-------+-------------+
| sid | sname | avg(number) |
+-----+-------+-------------+
| 2 | 铁锤 | 100.0000 |
+-----+-------+-------------+
1 row in set (0.00 sec)
-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
-- 16.查询各个课程及相应的选修人数
mysql> select cid,count(corse_id) from course left join score on corse_id = course.cid group by cid;
+-----+-------------------+
| cid | count(student_id) |
+-----+-------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 0 |
+-----+-------------------+
3 rows in set (0.00 sec)
-- 17.查询“4”课程分数小于60,按分数降序排列的同学学号
-- 18.删除学号为“2”的同学的“1”课程的成绩
答案
--所有表新建
# 班级表(class)
mysql> create table class(
-> cid int auto_increment primary key,
-> caption varchar(32) not null default ' '
-> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into clas(caption) values('三年二班'),('一年一班'),('三年一班');
ERROR 1146 (42S02): Table 'day34.clas' doesn't exist
mysql> insert into class(caption) values('三年二班'),('一年一班'),('三年一班');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from class;
+-----+----------+
| cid | caption |
+-----+----------+
| 1 | 三年二班 |
| 2 | 一年一班 |
| 3 | 三年一班 |
+-----+----------+
3 rows in set (0.00 sec)
# 老师表(teacher)
mysql> create table teacher(
-> tid int auto_increment primary key,
-> tname varchar(32) not null default ' '
-> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into teacher(tname) values('波多'),('苍空'),('饭岛';
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from teacher;
+-----+-------+
| tid | tname |
+-----+-------+
| 1 | 波多 |
| 2 | 苍空 |
| 3 | 饭岛 |
+-----+-------+
3 rows in set (0.00 sec)
# 学生表(student)
mysql> create table student(
-> sid int auto_increment primary key,
-> sname varchar(32) not null default ' ',
-> gender enum('男','女'),
-> class_id int, ,
-> constraint student_class foreign key(class_id) references class(cid)
-> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into student(sname,gender,class_id) values ('钢蛋','女',1),('铁锤n','女',1),('山炮','男',2);
mysql> select * from student;
+-----+-------+--------+----------+
| sid | sname | gender | class_id |
+-----+-------+--------+----------+
| 1 | 钢蛋 | 女 | 1 |
| 2 | 铁锤 | 女 | 1 |
| 3 | 山炮 | 男 | 2 |
+-----+-------+--------+----------+
3 rows in set (0.00 sec)
# 课程表(course)
mysql> create table course(
-> cid int auto_increment primary key,
-> cname varchar(32) not null default ' ',
-> tearch_id int,
-> constraint teacher_course foreign key(tearch_id) references teacher(tid)
-> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql> insert into course(cname,tearch_id) values ('生物',1),('体育',1),('物理',2;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from course;
+-----+-------+-----------+
| cid | cname | tearch_id |
+-----+-------+-----------+
| 1 | 生物 | 1 |
| 2 | 体育 | 1 |
| 3 | 物理 | 2 |
+-----+-------+-----------+
3 rows in set (0.00 sec)
# 成绩表(score)
mysql> create table score(
-> sid int auto_increment primary key,
-> student_id int,
-> corse_id int,
-> number int,
-> constraint score_student foreign key(student_id) references student(sid),
-> constraint score_course foreign key(corse_id) references course(cid)
-> )charset=utf8;
Query OK, 0 rows affected (0.14 sec)
mysql> insert into score(student_id,corse_id,number) values (1,1,60),(1,2,59),(2,2,100);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 1 | 1 | 1 | 60 |
| 2 | 1 | 2 | 59 |
| 3 | 2 | 2 | 100 |
+-----+------------+----------+--------+
3 rows in set (0.00 sec)