本系列文章:
Mysql(一)三大范式、数据类型、常用函数、事务
Mysql(二)Mysql SQL练习题
Mysql(三)索引、视图、存储过程、触发器、分区表
Mysql(四)存储引擎、锁
Mysql(五)Mysql架构、数据库优化、主从复制
Mysql(六)慢查询、执行计划、SQL语句优化
- 学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别- 课程表
Course(c_id,c_name,t_id) --课程编号, 课程名称, 教师编号- 教师表
Teacher(t_id,t_name) --教师编号,教师姓名- 成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数
#学生表
CREATE TABLE IF NOT EXISTS Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL,
s_birth VARCHAR(20) NOT NULL,
s_sex VARCHAR(10) NOT NULL,
PRIMARY KEY(s_id)
);
#课程表
CREATE TABLE IF NOT EXISTS Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
#教师表
CREATE TABLE IF NOT EXISTS Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL,
PRIMARY KEY(t_id)
);
#成绩表
CREATE TABLE IF NOT EXISTS Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id,c_id)
);
#插入学生表测试数据
insert into Student values('01' , 'zhaolei' , '1990-01-01' , 'man');
insert into Student values('02' , 'qiandian' , '1990-12-21' , 'man');
insert into Student values('03' , 'sunfeng' , '1990-05-20' , 'man');
insert into Student values('04' , 'liyun' , '1990-08-06' , 'man');
insert into Student values('05' , 'zhoumei' , '1991-12-01' , 'women');
insert into Student values('06' , 'wulan' , '1992-03-01' , 'women');
insert into Student values('07' , 'zhengzhu' , '1989-07-01' , 'women');
insert into Student values('08' , 'wangju' , '1990-01-20' , 'women');
#课程表测试数据
insert into Course values('01' , 'yuwen' , '02');
insert into Course values('02' , 'shuxue' , '01');
insert into Course values('03' , 'yingyu' , '03');
#教师表测试数据
insert into Teacher values('01' , 'zhangsan');
insert into Teacher values('02' , 'lisi');
insert into Teacher values('03' , 'wangwu');
#成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
select * from student where s_name like '%feng%';
select count(*) from teacher where t_name like 'li%';
select s_sex as '性别',count(s_sex) as '数量' from student group by s_sex;
select c_id as '课程',count(s_id) as '数量' from score group by c_id;
select c.c_id ,c.c_name as '课程名称',count(c.c_id) as '学生数量' from course c
join score on c.c_id = score.c_id
group by c.c_id;
select * from student
where week(date_format(now(),'%Y%m%d'))+1 = week(s_birth);
#与上一题类似的解法
select * from student
where month(date_format(now(),'%Y%m%d')) = month(s_birth);
select * from student
where month(date_format(now(),'%Y%m%d'))+1 = month(s_birth);
select s_name,s_birth from student where s_birth like '1990%';
select s_id,count(c_id) from Score
group by s_id having count(c_id) >= 2;
select * from Student
where s_id in (select s_id from Score group by s_id
having count(c_id) = (select count(*) from course));
select s_id,s_name from student
where s_id in(select s_id from score group by s_id
having count(c_id)=2);
select distinct sc2.*
from score sc1,score sc2
where sc1.c_id != sc2.c_id and sc1.s_score = sc2.s_score;
select stu.* from student stu,score sc1,score sc2
where stu.s_id = sc1.s_id
and stu.s_id = sc2.s_id
and sc1.c_id='01' and sc2.c_id='02';
select stu.* from student stu
where stu.s_id in (select s_id from score where c_id='01' )
and stu.s_id not in(select s_id from score where c_id='02');
select stu.* ,sco.s_score as 01_score,sco2.s_score as 02_score
from student stu
join score sco on stu.s_id = sco.s_id and sco.c_id='01'
join score sco2 on stu.s_id=sco2.s_id and sco2.c_id='02'
where sco.s_score>sco2.s_score;
select stu.* ,sco.s_score as 01_score,sco2.s_score as 02_score
from student stu
join score sco on stu.s_id = sco.s_id and sco.c_id='01'
join score sco2 on stu.s_id=sco2.s_id and sco2.c_id='02'
where sco.s_score < sco2.s_score;
select sc.s_id,stu.s_name,avg(sc.s_score) as avg_score
from score sc
join student stu on sc.s_id = stu.s_id
group by sc.s_id
having avg(sc.s_score) >= 60;
select stu.s_id,stu.s_name,round(avg(sc.s_score),2) as avg_score
from student stu
join score sc
on stu.s_id = sc.s_id
group by stu.s_id,stu.s_name
having round(avg(sc.s_score),2)<60
union
select stu.s_id,stu.s_name,0 as avg_score from student stu
where stu.s_id not in (select distinct s_id from score);
select stu.s_id,
stu.s_name,
count(sc.c_id) as sum_course,
sum(sc.s_score) as sum_score
from student stu
left join score sc
on stu.s_id = sc.s_id
group by stu.s_id;
select stu.* from student stu
join score sco
on stu.s_id = sco.s_id
where sco.c_id in(
select c_id from course where t_id =(
select t_id from teacher where t_name = 'zhangsan'
)
);
SELECT st.* from student st
left join score sc on sc.s_id=st.s_id
left join course c on c.c_id=sc.c_id
left join teacher t on t.t_id=c.t_id
where t.t_name='zhangsan';
select * from student c
where c.s_id not in(
select a.s_id from student a
join score b on a.s_id = b.s_id where b.c_id in(
select c_id from course where t_id = (
select t_id from teacher where t_name = 'zhangsan'
)
)
);
select s.* from student s
where s.s_id in(
select s_id from score where s_id not in(
select a.s_id from score a
join score b on a.s_id = b.s_id and b.c_id='02'
join score c on a.s_id = c.s_id and c.c_id='03'
where a.c_id='01'
)
)
select * from student where s_id in(
select distinct a.s_id
from score a
where a.c_id in (
select a.c_id from score a where a.s_id='01'
)
);
select a.* from student a where a.s_id in(
select distinct s_id from score
where s_id!='01' and c_id in(
select c_id from score where s_id='01'
)
group by s_id
having count(1) = (
select count(1) from score where s_id='01'
)
);
select a.s_name from student a
where a.s_id not in (
select s_id from score where c_id = (
select c_id from course where t_id = (
select t_id from teacher where t_name = 'zhangsan'
)
)
group by s_id
);
select a.s_id,a.s_name,round(avg(b.s_score)) from student a
left join score b on a.s_id = b.s_id
where a.s_id in(
select s_id from score where s_score<60
group by s_id having count(1)>=2
)
group by a.s_id,a.s_name
select a.*,b.c_id,b.s_score from student a,score b
where a.s_id = b.s_id and b.c_id='01' and b.s_score<60
order by b.s_score desc;
select a.s_id,
(select s_score from score where s_id=a.s_id and c_id='01') as 语文,
(select s_score from score where s_id=a.s_id and c_id='02') as 数学,
(select s_score from score where s_id=a.s_id and c_id='03') as 英语,
round(avg(s_score),2) as 平均分
from score a
group by a.s_id
order by 平均分 desc;
select a.t_id,c.t_name,a.c_id,round(avg(s_score),2) as avg_score
from course a
left join score b on a.c_id=b.c_id
left join teacher c on a.t_id=c.t_id
group by a.c_id,a.t_id,c.t_name
order by avg_score desc;
select sc1.s_id,sc1.c_id,sc1.s_score from score sc1
left join score sc2 on sc1.c_id = sc2.c_id and sc1.s_score<sc2.s_score
group by sc1.s_id,sc1.c_id,sc1.s_score
having count(sc2.s_id)<3
order by sc1.c_id,sc1.s_score desc;
select stu1.s_name,stu1.s_sex,count(*)
from student stu1
join student stu2 on stu1.s_id !=stu2.s_id
and stu1.s_name = stu2.s_name
and stu1.s_sex = stu2.s_sex
group by stu1.s_name,stu1.s_sex;
select c_id,avg(s_score) avg_sc from score
group by c_id
order by avg_sc desc,c_id asc;
select stu.s_name,stu.s_name,avg(sc.s_score) avg_sc
from student stu
join score sc on stu.s_id=sc.s_id
group by sc.s_id
having avg_sc >=85;
select stu.s_name,sc.s_score from student stu
join score sc on stu.s_id=sc.s_id
join course co on co.c_id=sc.c_id
where co.c_name = 'shuxue' and sc.s_score <60;
SELECT st.s_id,st.s_name
,MAX(CASE WHEN co.c_name='yuwen' THEN sc.s_score ELSE NULL END) AS 'yuwen'
,MAX(CASE WHEN co.c_name='shuxue' THEN sc.s_score ELSE NULL END) AS 'shuxue'
,MAX(CASE WHEN co.c_name='yingyu' THEN sc.s_score ELSE NULL END) AS 'yingyu'
FROM student AS st
LEFT JOIN score AS sc ON st.s_id=sc.s_id
LEFT JOIN course AS co ON sc.c_id=co.c_id
GROUP BY st.s_id,st.s_name;
select stu.s_name,co.c_name,sc.s_score from course co
left join score sc on co.c_id = sc.c_id
left join student stu on stu.s_id=sc.s_id
where sc.s_score>=70
select sc.*,co.c_name from score sc
left join course co on sc.c_id = co.c_id
where sc.s_score<60
select st.s_id,st.s_name from student st
join score sc
on sc.s_id = st.s_id and sc.c_id='01' and sc.s_score>=80;
select st.s_id,st.s_name from student st
join score sc on sc.s_id = st.s_id
where sc.c_id='01' and sc.s_score>=80;
SELECT stu.*,sc.s_score FROM student as stu
JOIN score as sc on stu.s_id=sc.s_id
JOIN course as co on sc.c_id=co.c_id
JOIN teacher as te ON te.t_id=co.t_id
WHERE te.t_name='zhangsan'
ORDER BY s_score DESC LIMIT 0,1;
select sc1.s_id,sc1.c_id,sc1.s_score from score sc1
where (
select count(s_score) from score sc2
where sc2.c_id = sc1.c_id and sc2.s_score >= sc1.s_score
) <= 2
order by sc1.c_id;
select c_id,count(s_id) num from Score
group by c_id
having num >5
order by num desc,c_id asc;
select s_birth,
(DATE_FORMAT(NOW(),'%Y') - DATE_FORMAT(s_birth,'%Y') -
(case when DATE_FORMAT(NOW(),'%m%d') > DATE_FORMAT(s_birth,'%m%d')
then 0 else 1 end
)
) as age
from student;
select * from student
where week(date_format(now(),'%Y%m%d')) = week(s_birth);