create table if not exists student
(
sid
int(10) comment '学生学号',
sname
varchar(5) comment '学生姓名',
sage
datetime comment '学生出生日期',
sgender
char(1) comment '学生性别'
)engine=innodb default charset=utf8
;
insert into student
values (01,'赵雷','1990-01-01','男');
insert into student
values (02,'钱电','1990-12-21','男'),
(03,'孙风','1990-12-20','男'),
(04,'李云','1990-12-06','男'),
(05,'周梅','1991-12-01','女'),
(06,'吴兰','1992-01-01','女'),
(07,'郑竹','1989-01-01','女'),
(08,'张三','2017-12-20','女'),
(09,'李四','2017-12-25','女'),
(10,'李四','2012-06-06','女'),
(11,'赵六','2013-06-13','女'),
(12,'孙七','2014-06-01','女');
create table course
(
cid
int(10) comment '科目号',
cname
varchar(10) comment '科目名称',
tid
int(10) comment '授课老师'
)engine=innodb default charset=utf8
;
insert into course
values (01,'语文',02),(02,'数学',01),(03,'英语',03);
create table teacher
(
tid
int(10) comment '教师学号',
tname
varchar(10) comment '教师姓名'
)engine=innodb default charset=utf8
;
insert into teacher
values(01,'张三'),(02,'李四'),(03,'王五');
create table score
(
sid
int(10) comment '学生学号',
cid
int(10) comment '科目号',
grade
decimal(18,1)
)engine=innodb default charset=utf8
;
insert into score
values (01,01,80),
(01,02,90),
(01,03,99),
(02,01,70),
(02,02,60),
(02,03,80),
(03,01,90),
(03,02,80),
(03,03,80),
(04,01,50),
(04,02,30),
(05,01,76),
(05,02,87),
(06,01,31),
(06,03,34),
(07,02,89),
(07,03,98);
select
s
.sid
,sname
,sage
,sgender
,class1
,class2
from student s
inner join (
select a
.sid
,a
.grade
as class1
,b
.grade
as class2
from score a
inner join score b
on a
.sid
=b
.sid
where a
.cid
=01 and b
.cid
=02 and a
.grade
>b
.grade
) r
on s
.sid
=r
.sid
;
select s
.sid
,sname
,sage
,sgender
,class1
,class2
from student s
inner join(
select a
.sid
,class1
,class2
from
(select sid
,grade
as class1
from score
where score
.cid
=01) a
,
(select sid
,grade
as class2
from score
where score
.cid
=02) b
where a
.sid
=b
.sid
and class1
>class2
) r
on s
.sid
=r
.sid
;
select * from
(select * from score
where score
.cid
=01) a
,
(select * from score
where score
.cid
=02) b
where a
.sid
=b
.sid
;
select * from
(select * from score
where score
.cid
=01) a
left join
(select * from score
where score
.cid
=02) b
on a
.sid
=b
.sid
;
select * from
(select * from score
where score
.cid
=01) a
right join
(select * from score
where score
.cid
=02) b
on a
.sid
=b
.sid
;
select * from score
where score
.sid
not in(
select sid
from score
where score
.cid
=01
)
and score
.cid
=02;
select s
.sid
,sname
,avg_grade
from student s
inner join(
select sid
,avg(grade
) as avg_grade
from score
group by sid
having avg_grade
>60
) r
on s
.sid
=r
.sid
;
select s
.sid
,sname
,sage
,sgender
from student s
inner join (
select distinct sid
from score
) r
on s
.sid
=r
.sid
;
select s
.sid
,sname
,count_cid
,sum_grade
from student s
left join (
select sid
, count(cid
) as count_cid
, sum(grade
) as sum_grade
from score
group by sid
) r
on s
.sid
=r
.sid
;
select s
.sid
,sname
,count_cid
,sum_grade
from student s
,(
select sid
, count(cid
) as count_cid
, sum(grade
) as sum_grade
from score
group by sid
) r
where s
.sid
=r
.sid
;
select s
.sid
,sname
,sage
,sgender
from student s
,(
select distinct sid
from score
) r
where s
.sid
=r
.sid
;
select * from student s
where s
.sid
in(select distinct sid
from score
);
select count(*)
from teacher
where tname
like '李%';
select student
.*
from student
,teacher
,course
,score
where teacher
.tname
='张三'
and teacher
.tid
=course
.tid
and course
.cid
=score
.cid
and score
.sid
=student
.sid
;
select student
.* from student
where student
.sid
not in(
select sid
from score
group by sid
having count(cid
)=(select count(cid
) from course
)
);
select * from student
where sid
in(
select distinct sid
from score
where cid
in(
select cid
from score
where sid
=01
)
);
select * from student
where sid
in(
select sid
from score
where cid
in(
select cid
from score
where sid
=01
)
group by sid
) and sid
in(
select sid
from score
group by sid
having count(cid
)=(
select count(cid
) from score
where sid
=01
)
);
select * from student
where sid
not in(
select sid
from score
where cid
in(
select cid
from course
where tid
in(
select tid
from teacher
where tname
='张三'
)
)
);
select * from student
where sid
not in(
select sid
from score
,course
,teacher
where teacher
.tname
='张三'
and teacher
.tid
=course
.tid
and score
.cid
=course
.cid
);
select s
.sid
,sname
,avg_grade
from student s
inner join (
select sid
, avg(grade
) as avg_grade
from score
where grade
<60
group by sid
having count(cid
)>1
) r
on s
.sid
=r
.sid
select s
.sid
,sname
,sage
,sgender
,grade
from student s
inner join(
select sid
,grade
from score
where grade
<60 and cid
=01
order by grade
desc
) r
on s
.sid
=r
.sid
;
select student
.*,grade
from student
, score
where score
.cid
=01 and score
.grade
<60
and student
.sid
=score
.sid
order by score
.grade
desc;
select score
.sid
,grade
,avg_grade
from score
left join(
select sid
,avg(grade
) as avg_grade
from score
group by sid
) r
on score
.sid
=r
.sid
order by avg_grade
desc;
select cid
,max(grade
) as max_grade
, min(grade
) as min_grade
, avg(grade
) as avg_grade
from score
group by cid
;
select r
.*,cname
from course c
inner join (
select cid
,max(grade
) as 最高分
, min(grade
) as 最低分
, avg(grade
) as 平均分
, count(sid
) as 选课人数
from score
group by cid
) r
on c
.cid
=r
.cid
;
set @rank=0;
select cid
,sid
,grade
,@rank:
=@rank+1 as 排名
from (
select cid
,sid
,grade
from score
where cid
=01
order by grade
desc
) r
;
select a
.cid
, a
.sid
, a
.grade
, count(b
.grade
)+1 as rank
from score
as a
left join score
as b
on a
.grade
<b
.grade
and a
.cid
= b
.cid
group by a
.cid
, a
.sid
,a
.grade
order by a
.cid
, rank
ASC;
set @rank=0;
select sid
,总分
,@rank:
=@rank+1 as 排名
from (
select sid
,sum(grade
) as 总分
from score
group by sid
order by 总分
desc
) r
;
select s
.cid
,cname
,
sum(case when grade
<=100 and grade
>85 then 1 else 0 end) as '[100-85]',
sum(case when grade
<=85 and grade
>70 then 1 else 0 end) as '[85-70]',
sum(case when grade
<=70 and grade
>60 then 1 else 0 end) as '[70-60]',
sum(case when grade
<=60 and grade
>0 then 1 else 0 end) as '[60-0]'
from score s
left join course c
on s
.cid
=c
.cid
group by s
.cid
;
select * from (
select *,row_number
() over(partition by cid
order by grade
desc) as rank
from score
) r
where rank
<4;
select cid
,count(sid
) from score
group by cid
;
select sid
,sname
from student
where sid
in(
select sid
from score
group by sid
having count(cid
)=2
);
select sgender
,count(*) from student
group by sgender
;
select * from student
where sname
like '%风%';
select sname
,count(sname
) as 同名人数
from student
group by sname
having count(sname
)>1
select * from student s
where sname
in (
select sname
from student
group by sname
having count(sname
)>1
);
select * from student
where year(sage
)='1990';
select score
.cid
,cname
,avg(grade
) as 平均分
from course
,score
where course
.cid
=score
.cid
group by course
.cid
order by 平均分
desc,course
.cid
asc
select s
.sid
,sname
,平均成绩
from student s
inner join (
select sid
,avg(grade
) as 平均成绩
from score
group by sid
having 平均成绩
>=85
) r
on s
.sid
=r
.sid
;
select a
.sid
,sname
,avg(grade
) as 平均成绩
from student a
, score b
where a
.sid
=b
.sid
group by b
.sid
having 平均成绩
>=85;
select sname
,grade
from student s
inner join (
select sid
,grade
from score
where cid
in (
select cid
from course
where cname
='数学'
)
and grade
<60
) r
on s
.sid
=r
.sid
;
select s
.sid
,sname
,cid
,grade
from student s
left join score c
on s
.sid
=c
.sid
;
select sname
,cname
,grade
from student a
,course b
,(
select * from score
where grade
>70
group by sid
,cid
) r
where a
.sid
=r
.sid
and b
.cid
=r
.cid
select sname
,cname
,grade
from student a
,course b
,score c
where c
.grade
>70
and a
.sid
=c
.sid
and b
.cid
=c
.cid
select cid
from score
where grade
<60
group by cid
select a
.sid
,sname
from student a
,score b
where b
.cid
=01
and grade
>80
and a
.sid
=b
.sid
select cid
,count(*) as 学生人数
from score
group by cid
select student
.sid
,sname
,grade
from student
,score
where cid
=(
select cid
from course
where tid
=(
select tid
from teacher
where tname
='张三'
)
)
and student
.sid
=score
.sid
order by grade
desc
limit 1;
update score
set grade
=90 where sid
=07 and cid
=02;
select a
.sid
,sname
,grade
from student a
,teacher b
,course c
,score d
where b
.tname
='张三'
and b
.tid
=c
.tid
and c
.cid
=d
.cid
and d
.sid
=a
.sid
and d
.grade
=(
select max(grade
) from student
,teacher
,course
,score
where teacher
.tname
='张三'
and teacher
.tid
=course
.tid
and course
.cid
=score
.cid
and score
.sid
=student
.sid
);
select a
.sid
,a
.cid
,a
.grade
from score a
inner join score b
on a
.sid
=b
.sid
and a
.cid
!=b
.cid
and a
.grade
=b
.grade
;
select a
.sid
,a
.cid
,a
.grade
from score
as a
left join score
as b
on a
.cid
= b
.cid
and a
.grade
<b
.grade
group by a
.cid
, a
.sid
having count(b
.cid
)<2
order by a
.cid
;
select cid
,count(sid
)
from score
group by cid
having count(sid
)>5;
select sid
from score
group by sid
having count(cid
)>=2;
select * from student
where sid
in(
select sid
from score
group by sid
having count(cid
)=(
select count(cid
) from course
)
);
select sid
,sname
,timestampdiff
(year,sage
,curdate
()) as 学生年纪
from student
;
select * from student
where weekofyear
(sage
)=weekofyear
(curdate
());
select * from student
where weekofyear
(sage
)=weekofyear
(curdate
())+1;
select * from student
where month(sage
)=month(curdate
());
select * from student
where month(sage
)=month(curdate
())+1;
转载请注明原文地址:https://tech.qufami.com/read-2034.html