drop database if exists stu;
create database stu;
create table student(
s_id int(10) not null auto_increment comment '学生编号',
s_name varchar(10) not null comment '学生姓名',
s_birth varchar(20) not null comment '出生日期',
s_sex varchar(2) not null comment '性别',
primary key(s_id)
) engine=innodb default charset=utf8;
create table student(
s_id int(10) not null auto_increment comment '学生编号',
s_name varchar(10) not null comment '学生姓名',
s_birth varchar(20) not null comment '出生日期',
s_sex varchar(2) not null comment '性别',
primary key(s_id)
) engine=innodb default charset=utf8;
create table course(
c_id int(10) not null auto_increment comment '课程编号',
c_name varchar(10) not null comment '课程名称',
t_id int(10) not null comment '任课老师',
primary key(c_id)
) engine=innodb default charset=utf8;
create table teacher(
t_id int(10) not null auto_increment comment '教师编号',
t_name varchar(10) not null comment '教师姓名',
primary key(t_id)
) engine=innodb default charset=utf8;
create table score(
s_id int(10) not null auto_increment comment '成绩编号',
c_id int(10) not null comment '课程编号',
s_score int(3) not null comment '学生分数',
primary key(s_id,c_id)
) engine=innodb default charset=utf8;
insert into Student values(1, '赵雷' , '1990-01-01' , '男');
insert into Student values(2, '钱电' , '1990-12-21' , '男');
insert into Student values(3, '孙风' , '1990-05-20' , '男');
insert into Student values(4, '李云' , '1990-08-06' , '男');
insert into Student values(5, '周梅' , '1991-12-01' , '女');
insert into Student values(6, '吴兰' , '1992-03-01' , '女');
insert into Student values(7, '郑竹' , '1989-07-01' , '女');
insert into Student values(8, '王菊' , '1990-01-20' , '女');
insert into Course values(1, '语文' , 2);
insert into Course values(2, '数学' , 1);
insert into Course values(3, '英语' , 3);
insert into Teacher values(1 , '张三');
insert into Teacher values(2 , '李四');
insert into Teacher values(3 , '王五');
insert into Score values(1 , 1 , 80);
insert into Score values(1 , 2 , 90);
insert into Score values(1 , 3 , 99);
insert into Score values(2 , 1 , 70);
insert into Score values(2 , 2 , 60);
insert into Score values(2 , 3 , 80);
insert into Score values(3 , 1 , 80);
insert into Score values(3 , 2 , 80);
insert into Score values(3 , 3 , 80);
insert into Score values(4 , 1 , 50);
insert into Score values(4 , 2 , 30);
insert into Score values(4 , 3 , 20);
insert into Score values(5 , 1 , 76);
insert into Score values(5 , 2 , 87);
insert into Score values(6 , 1 , 31);
insert into Score values(6 , 3 , 34);
insert into Score values(7 , 2 , 89);
insert into Score values(7 , 3 , 98);
select a.* , b.s_score as chinese , c.s_score as math from
student a
join score b on b.c_id = 1 and a.s_id = b.s_id
left join score c on c.c_id = 2 and a.s_id = c.s_id where b.s_score > c.s_score;
select a.* , b.s_score as chinese , c.s_score as math from
student a
join score b on b.c_id = 1 and a.s_id = b.s_id
left join score c on c.c_id = 2 and a.s_id = c.s_id
where b.s_score < c.s_score;
select b.s_id , b.s_name,round(avg(a.s_score),1) as avg_score
from student b
join score a on b.s_id = a.s_id
group by b.s_id,b.s_name having round(avg(a.s_score),1)>=60;
select b.s_id,b.s_name,round(avg(a.s_score)) as avg_score
from student b
left join score a on b.s_id = a.s_id
group by b.s_id,b.s_name having round(avg(a.s_score),2)<60
union
select a.s_id,a.s_name,0 as avg_score
from student a
where a.s_id not in (select distinct s_id from score);
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score
from student a
left join score b on a.s_id = b.s_id
group by a.s_id,a.s_name;