MySQL使用实例

Posted 91joe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL使用实例相关的知识,希望对你有一定的参考价值。

create database school charset utf8;
use school;
# 建立class表
create table class(
cid int unsigned not null auto_increment ,
caption varchar(50) not null unique ,
grade_id int unsigned not null ,
PRIMARY KEY(cid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC class;
# 建立student表
CREATE TABLE student(
`sid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`sname` VARCHAR(50) NOT NULL ,
`gender` ENUM(‘男‘,‘女‘) NOT NULL DEFAULT ‘男‘,
`class_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `student`;
# 建立teacher表
CREATE TABLE `teacher`(
tid INT UNSIGNED NOT NULL AUTO_INCREMENT ,
tname VARCHAR(50) NOT NULL ,
PRIMARY KEY (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `teacher`;
# 建立course表
CREATE TABLE `course`(
`cid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`cname` VARCHAR(50) NOT NULL ,
`teacher_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC course;
# 建立score表
CREATE TABLE `score`(
`sid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`student_id` INT UNSIGNED NOT NULL,
`course_id` INT UNSIGNED NOT NULL,
`score` INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `score`;
# 建立class_grade表
CREATE TABLE `class_grade`(
`gid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`gname` VARCHAR(50) NOT NULL,
PRIMARY KEY (`gid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `class_grade`;
# 建立 teacher2cls表
CREATE TABLE `teach2cls`(
`tcid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`tid` INT UNSIGNED NOT NULL,
`cid` INT UNSIGNED NOT NULL,
PRIMARY KEY (`tcid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `class_grade`;
# 插入数据
-- cls
INSERT INTO `class`(`caption`, `grade_id`) VALUES
(‘一年一班‘, 1),
(‘二年一班‘, 2),
(‘三年二班‘, 3),
(‘六年一班‘, 6);
--stu
INSERT INTO `student`(`sname`, `gender`, `class_id`) VALUES
(‘乔丹‘, ‘女‘, 1),
(‘Iverson‘,‘女‘,1),
(‘科比‘,‘男‘,2),
(‘郭达斯坦森‘, ‘男‘, 2),
(‘蔡许昆‘, ‘女‘, 3),
(‘Egon‘, ‘女‘, 4);
--teach
INSERT INTO `teacher`(`tname`) VALUES
(‘张三‘),
(‘李四‘),
(‘王五‘),
(‘刘能‘);
--course
INSERT INTO `course`(`cname`, `teacher_id`) VALUES
(‘生物‘, 1),
(‘体育‘, 1),
(‘物理‘, 2),
(‘数学‘, 3),
(‘英语‘, 4);
--score
INSERT INTO `score`(`student_id`, `course_id`, `score`) VALUES
(1, 1, 67),
(1, 2, 36),
(2, 2, 99),
(2, 3, 96),
(2, 4, 73),
(3, 1, 100),
(4, 5, 95),
(6, 3, 84),
(1, 3, 93),
(2, 1, 39);
--grade
INSERT INTO `class_grade`(`gname`) VALUES
(‘一年级‘),
(‘二年级‘),
(‘三年级‘),
(‘四年级‘),
(‘五年级‘),
(‘六年级‘);
--teacher2cls
INSERT INTO `teach2cls`(`tid`, `cid`) VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 2),
(4, 3),
(1, 3),
(2, 4),
(4, 5);

 

 

-- 1、查找学生总人数
select count(sid) as ‘student_num‘ from student;

-- 2、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
select sid, sname from student where sid in (
select
score.student_id # 找到学习了这两们课且都及格学生的id
from
score
inner join course
on score.course_id=course.cid
where
course.cname in (
‘生物‘,
‘物理‘
)
and score.score >=60
group by
score.student_id
having
count(course_id) =2
);

-- 3、查询每个年级的班级数,取出班级数最多的前三个年级;
select
gname,count(class.cid)
from
class_grade
inner join
class
on class.grade_id=class_grade.gid
group by gid
order by count(cid) DESC
limit 3;

-- 4、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
select
student.sid, student.sname, AVG(score.score)
from
score
inner join
student
on
score.student_id = student.sid
group by
score.student_id
having AVG(score.score) in (
(select AVG(score.score) as ‘score_a‘
from student inner join score on student.sid = score.student_id
group by student.sid
order by score_a DESC
limit 1),
(select AVG(score.score) as ‘score_a‘
from student inner join score on student.sid = score.student_id
group by student.sid
order by score_a
limit 1));

-- 5、查询每个年级的学生人数;
select
class_grade.gname, count(student.sid) as ‘student_c‘
from
(class right join class_grade ON class.grade_id = class_grade.gid)
left join student ON class.cid = student.class_id
group by gname
order by gid;

-- 6、查询每位学生的学号,姓名,选课数,平均成绩;
select
student.sid,
concat(student.sname),
count(course_id) as course_num,
avg(score) as avg_score
from
student
inner join
score
on student.sid=score.student_id
group by student.sid;

-- 7、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
# score-student-course
select
student.sname,
course.cname,
score.score
from
score
left join student on score.student_id=student.sid
left join course on score.course_id=course.cid
where student.sid=2
and score in (
select * from (
select
max(score)
from
score
where
student_id=2) t1
union
select * from (
select
min(score)
from
score
where
student_id=2) t2
);

-- 8、查询姓“李”的老师的个数和所带班级数;
select
concat(teacher.tname),
count(distinct teacher.tid) as teacher_num,
count(teach2cls.cid) as class_num
from
teacher
inner join
teach2cls
on teacher.tid=teach2cls.tid
where
tname like ‘李%‘
group by teacher.tid;

-- 9、查询班级数小于5的年级id和年级名;
select
gid,
gname
from
class_grade
where gid in (
select
grade_id
from
class
group by
grade_id
having
count(cid) < 5
);

-- 10、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
# mysql中case when then else end的用法
select
class.cid,
class.caption,
class_grade.gname,
case # 如果
when class_grade.gid between 1 and 2 then ‘低年级‘ # when后接条件,then后接返回值
when class_grade.gid between 3 and 4 then ‘中年级‘
when class_grade.gid between 5 and 6 then ‘高年级‘
end as ‘年级级别‘ # end结束
from
class
inner join
class_grade
on class.grade_id=class_grade.gid;

-- 11、查询学过“张三”老师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(course_id)>=2
);

-- 12、查询教授课程超过2门的老师的id和姓名;
select
tid,
tname
from
teacher
where tid in (
select
teacher_id
from
course
group by
teacher_id
having
count(cid) >= 2
);

-- 13、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
select
sid,
sname
from
student
where sid in (
select
student_id
from
score
where course_id in (1,2)
group by
student_id
);

-- 14、查询没有带过高年级的老师id和姓名;
select
*
from
teacher
where tid not in (
select
tid
from
teach2cls
inner join
class
on teach2cls.cid=class.cid
where class.grade_id in (5,6)
);

-- 15、查询学过“张三”老师所教的所有课的同学的学号、姓名;
select
sid,
sname
from
student
where class_id in (
select
cid # 班级id
from
teach2cls
inner join
teacher
on teach2cls.tid=teacher.tid
where teacher.tname=‘张三‘
);

-- 16、查询带过超过2个班级的老师的id和姓名;
select
*
from
teacher
where tid in (
select
tid
from
teach2cls
group by
tid
having
count(cid) >= 2
);

-- 17、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
select
sid,
sname
from
student
where sid in(
select
t1.student_id
from
(select
student_id,
score as score_1
from
score
where
course_id=1) as t1
inner join
(select
student_id,
score as score_2
from
score
where
course_id=2) as t2
on t1.student_id=t2.student_id
where
t2.score_2 > t1.score_1
);

-- 18、查询所带班级数最多的老师id和姓名;
select
*
from
teacher
where tid in (
select
tid # 考虑带最多班级的是多个老师,老师的tid
from
teach2cls
group by
tid
having
count(cid)=(
select # 得到最多班级数量
count(cid)
from
teach2cls
group by
tid
order by
count(cid) desc
limit 1
)
);

-- 19、查询有课程成绩小于60分的同学的学号、姓名;
select
sid,
sname
from
student
where
sid in (
select
distinct student_id
from
score
where
score.score < 60
);

-- 20、查询没有学全所有课的同学的学号、姓名;
select
sid,
sname
from
student
where sid in (
select
student_id
from
score
group by student_id
having count(course_id) < (
select
count(cid)
from
course
)
);

-- 21、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
select
sid,
sname
from
student
where sid in (
select
distinct student_id
from
score
where course_id in (
select
course_id # 找出学号1锁学得所有课程
from
score
where
student_id=1
) and student_id!=1 # 排除掉学号为1的学生
);

-- 22、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
select
sid,
sname
from
student
where sid in (
select
distinct student_id
from
score
where course_id in (
select
course_id # 找出学号1锁学得所有课程
from
score
where
student_id=1
) and student_id!=1 # 排除掉学号为1的学生
);

-- 23、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
select
sid,
sname
from
student
where
sid in (
select
score.student_id
from
score
inner join
(select
course_id # 课程id
from
score
where
student_id = 2) as t1
where
score.course_id = t1.course_id
and
score.student_id != 2
group by
score.student_id
having
count(score.course_id) = (
select # 学号2所学课程总数
count(course_id)
from
score
where
student_id =2
)
);

-- 24、删除学习“张三”老师课的score表记录;
delete from
score
where
course_id in (
select
course.cid
from
course
left join
teacher
on course.teacher_id=teacher.tid
where
teacher.tname=‘张三‘
);

-- 25、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
insert into score (student_id, course_id, score)
select
(t1.sid,
2,
t2.avg_score)
from
(select
sid
from
student
where
sid not in (
select
student_id
from
score
where
course_id =2
)
) as t1,
(select
avg(score) as avg_score
from
score
where
course_id = 2
) as t2;

-- 26、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
select
main_score.student_id,
(select
score.score
from
score
left join
course
on score.course_id=course.cid
where
course.cname=‘语文‘
and score.student_id=main_score.student_id) as chinese,
(select
score.score
from
score
left join
course
on score.course_id=course.cid
where
course.cname=‘数学‘
and score.student_id=main_score.student_id) as math,
(select
score.score
from
score
left join
course
on score.course_id=course.cid
where
course.cname=‘英语‘
and score.student_id=main_score.student_id) as english,
count(main_score.course_id),
avg(main_score.score)
from
score as main_score
group by
main_score.student_id
order by # 注意order的拼写
student_id asC;

-- 27、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select
course_id,
max(score.score) as max_score,
min(score.score) as min_score
from
course
left join
score
on course.cid=score.course_id
group by
score.course_id;

-- 28、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
select
course_id,
avg(score) as avg_score
from
score
group by
course_id
order by
avg(score) desc;

-- 29、课程平均分从高到低显示(显示任课老师);
select
t2.cid,
t2.cname,
t2.avg_score,
teacher.tname
from
teacher
right join (
select
course.cid,
course.cname,
t1.avg_score,
course.teacher_id
from
course
inner join (
select
course_id,
avg(score) avg_score
from
score
group by
course_id
) as t1
on course.cid=t1.course_id
) as t2
on teacher.tid=t2.teacher_id
order by
t2.avg_score desc;

-- 30、查询各科成绩前三名的记录(不考虑成绩并列情况)
select
course.cid, course.cname, s2.first_s, s2.second_s, s2.third_s
from
(select course_id,
(select score from score as s2 where s1.course_id = s2.course_id order by score DESC limit 0,1) as ‘first_s‘,
(select score from score as s2 where s1.course_id = s2.course_id order by score DESC limit 1,1) as ‘second_s‘,
(select score from score as s2 where s1.course_id = s2.course_id order by score DESC limit 2,1) as ‘third_s‘
from score as s1
group by s1.course_id) as s2 right join course on s2.course_id = course.cid;

-- 31、查询每门课程被选修的学生数;
select
course_id,
count(student_id)
from
score
group by
course_id;

-- 32、查询选修了2门以上课程的全部学生的学号和姓名;
select
sid,
sname
from
student
where sid in (
select
student_id
from
score
group by
student_id
having count(course_id) > 2
);


-- 33、查询男生、女生的人数,按倒序排列;
select
gender,
count(sid)
from
student
group by gender
order by count(sid) desc;

-- 34、查询姓“张”的学生名单;
select
*
from
student
where
sname=‘张%‘;

-- 35、查询同名同姓学生名单,并统计同名人数;
select
sname,
count(sid)
from
student
group by
sname
having
count(sid) > 1;

-- 36、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select
course_id,
avg(score)
from
score
group by
course_id
order by
avg(score), # 两种排序规则需要用逗号分隔
course_id DESC;

-- 37、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select
student.sname,
score.score
from
score
inner join
student
on score.student_id=student.sid
where score.course_id in (
select
cid
from
course
where
cname=‘数学‘
);

-- 38、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
select
sid,
sname
from
student
where sid in (
select
student_id
from
score
where
course_id=3
and score>=80
);

-- 39、求选修了课程的学生人数
select
course_id,
count(student_id)
from
score
group by
course_id;

-- 40、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
select student.sname, t2.course_id, t2.score, t2.max_score, t2.min_score
from student
inner join
(select score.student_id, score.course_id, score.score, t1.max_score, t1.min_score
from score,
(select course_id, MAX(score) as ‘max_score‘ ,Min(score) as ‘min_score‘
from score
where course_id in
( select cid
from course
inner join teacher ON course.teacher_id = teacher.tid
where teacher.tname = ‘王五‘)
group by course_id) as t1
where score.course_id = t1.course_id and score.score in (max_score, min_score)
) as t2 ON student.sid = t2.student_id;

-- 41、查询各个课程及相应的选修人数;
select
cid,
cname,
t1.stu_num
from
course
right join
(select
course_id,
count(student_id) as stu_num
from
score
group by
course_id) as t1
on course.cid=t1.course_id;

-- 42、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select
*
from
score
where
score in (
select
score # 找到不同课程但是有相同分数的成绩数值
from
score
group by
score
having
count(course_id)>1
)
order by score;

-- 43、查询每门课程成绩最好的前两名学生id和姓名;
select student.sid, student.sname, t2.score,t2.course_id
from
(select score.student_id, score.course_id, score.score
from
(select
s1.course_id,
(select s2.score from score as s2 where s1.course_id = s2.course_id order by s2.score desc limit 0,1) as first_score,
(select s3.score from score as s3 where s1.course_id = s3.course_id order by s3.score desc limit 1,1) as second_score
from
score as s1
group by s1.course_id) as t1 inner join score on t1.course_id = score.course_id
where score.score in (t1.first_score, t1.second_score)
) as t2 inner join student on student.sid = t2.student_id
order by t2.course_id , t2.score DESC;

-- 44、检索至少选修两门课程的学生学号;
select
student_id
from
score
group by
student_id
having
count(course_id)>=2;

-- 45、查询没有学生选修的课程的课程号和课程名;
select
cid,
cname
from
course
where
cid not in (
select
course_id
from
score
group by
course_id
);

-- 46、查询没带过任何班级的老师id和姓名;
select
tid,
tname
from
teacher
where
tid not in(
select
tid
from
teach2cls
group by
tid
);

-- 47、查询有两门以上课程超过80分的学生id及其平均成绩;
select
student_id,
avg(score) as avg_score
from
score
where score.student_id in
(select
student_id # 找到有两门课大于80的学号
from
(select
*
from
score
where
score>80) as t1
group by
student_id
having
count(course_id) >= 2
)
group by student_id;

-- 48、检索“3”课程分数小于60,按分数降序排列的同学学号;
select
student_id
from
score
where
course_id=3
and score<60
order by score DESC;

-- 49、删除编号为“2”的同学的“1”课程的成绩;
delete from
score
where
sid in (
select
t1.sid
from (
select
sid
from
score
where
student_id=2
and course_id=1
) as t1
);

-- 50、查询同时选修了物理课和生物课的学生id和姓名;
select sid, sname
from student
where sid in
(select student_id
from score
where course_id in
(select cid
from course
where course.cname in (‘物理‘,‘生物‘))
group by student_id
having count(course_id) = 2
);

以上是关于MySQL使用实例的主要内容,如果未能解决你的问题,请参考以下文章

mysql多实例安装

MySQL运维-多实例部署

mysql-多实例

MySQL多实例配置

MySQL5.5多实例编译安装——多配置文件

MySQL5.5多实例编译安装——多配置文件