MySQL练习
-- #今天是MySQL练习题,要求用MySQL写
-- 偷偷转载一下,没人发现吧:http://www.cnblogs.com/wupeiqi/articles/5748496.html
-- 1、自行创建测试数据
CREATE DATABASE school;
CREATE TABLE 班级表:class(
cid INT PRIMARY KEY AUTO_INCREMENT,
caption VARCHAR (10)
)ENGINE=InnoDB;
CREATE TABLE 学生表:student(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR (10),
gender CHAR(1),
class_id TINYINT(1)
)ENGINE=InnoDB;
CREATE TABLE 老师表:teacher(
tid INT PRIMARY KEY AUTO_INCREMENT,
tname VARCHAR (10)
)ENGINE=InnoDB;
CREATE TABLE 课程表:course(
cid INT PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR (10),
teacher_id TINYINT(1)
)ENGINE=InnoDB;
CREATE TABLE 成绩表:score(
sid INT PRIMARY KEY AUTO_INCREMENT,
student_id TINYINT(1),
corse_id TINYINT(1),
number1 INT
)ENGINE=InnoDB;
INSERT INTO 班级表:class VALUES
(1,'三年二班'),
(2,'一年三班'),
(3,'三年一班');
INSERT INTO 学生表:student VALUES
(1,'钢蛋','女',1),
(2,'铁锤','女',1),
(3,'山炮','男',2);
INSERT INTO 老师表:teacher VALUES
(1,'波多'),
(2,'苍空'),
(3,'饭岛');
INSERT INTO 课程表:course VALUES
(1,'生物',1),
(2,'体育',1),
(3,'物理',2);
INSERT INTO 成绩表:score VALUES
(1,1,1,60),
(2,1,2,59),
(3,2,2,100);
-- 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;(我觉得这个题目有问题,根本就没有物理的成绩....-3-)
-- SELECT * FROM 成绩表:score,课程表:course WHERE 成绩表:score.corse_id = 课程表:course.cid AND
SELECT * FROM 学生表:student,课程表:course,成绩表:score W
HERE 学生表:student.sid = 成绩表:score.student_id AND
课程表:course.cid = 成绩表:score.corse_id;
-- 3、查询平均成绩大于60分的同学的学号和平均成绩;
select student_id,avg(number1) from 成绩表:score group by student_id having avg(number1) > 60;
-- 4、查询所有同学的学号、姓名、选课数、总成绩;
select 成绩表:score.student_id,sum(成绩表:score.number1),count(成绩表:score.student_id),学生表:student.sname
from
成绩表:score left join 学生表:student on 成绩表:score.student_id = 学生表:student.sid
group by 成绩表:score.student_id;
-- 5、查询姓“李”的老师的个数;
select count(tid) from 老师表:teacher where tname like '李%';
-- 6、查询没学过“叶平”老师课的同学的学号、姓名;
SELECT * FROM 学生表:student WHERE 学生表:student.sid not in (
select DISTINCT 成绩表:score.student_id from 成绩表:score where 成绩表:score.corse_id in (
select 成绩表:score.corse_id from 成绩表:score , 老师表:teacher
where 老师表:teacher.tname = '叶平老师'
)
)
-- 7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
SELECT student_id,corse_id FROM
(select student_id,corse_id from 成绩表:score where corse_id = 1 or corse_id = 2) as B
left join 学生表:student on B.student_id = 学生表:student.sid group by student_id HAVING count(student_id) > 1
-- 8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT * FROM 学生表:student WHERE 学生表:student.sid in (
select DISTINCT 成绩表:score.student_id from 成绩表:score where 成绩表:score.corse_id in (
select 成绩表:score.corse_id from 成绩表:score , 老师表:teacher
where 老师表:teacher.tname = '叶平老师'
)
)
-- 9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
SELECT A.student_id,A.sname FROM
(SELECT student_id,corse_id,number1,cname,sname FROM 成绩表:score,课程表:course,学生表:student
WHERE 成绩表:score.sid = 学生表:student.sid AND corse_id = cid AND corse_id = 1) as A
LEFT JOIN
(SELECT student_id,corse_id,number1,cname,sname FROM 成绩表:score,课程表:course,学生表:student
WHERE 成绩表:score.sid = 学生表:student.sid AND corse_id = cid AND corse_id = 2) as B
ON A.student_id = B.student_id WHERE A.number1>B.number1
-- 有bug,小朋友们谨慎使用
-- 10、查询有课程成绩小于60分的同学的学号、姓名;
SELECT student_id,sname FROM 学生表:student,成绩表:score WHERE class_id = corse_id AND number1 <60
-- 11、查询没有学全所有课的同学的学号、姓名;
select student_id,sname
from 成绩表:score left join 学生表:student on 成绩表:score.student_id = 学生表:student.sid
group by student_id HAVING count(corse_id) = (select count(1) from 课程表:course)
-- 12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
select student_id,sname, count(corse_id)
from 成绩表:score left join 学生表:student on 成绩表:score.student_id = 学生表:student.sid
where student_id != 1 and corse_id in (select corse_id from 成绩表:score where student_id = 1) group by student_id
Win a contest, win a challenge
posted on 2018-04-04 16:10 pandaboy1123 阅读(269) 评论(0) 编辑 收藏 举报