表和字段java
1.學生表
Student(s_id,s_name,s_birth,s_sex) –學生編號,學生姓名, 出生年月,學生性別 sql
2.課程表
Course(c_id,c_name,t_id) – –課程編號, 課程名稱, 教師編號數據庫
3.教師表
Teacher(t_id,t_name) –教師編號,教師姓名測試
4.成績表
Score(s_id,c_id,s_score) –學生編號,課程編號,分數spa
測試數據3d
--學生表 DROP TABLE IF EXISTS `student`; CREATE TABLE `student`( `s_id` VARCHAR(20), `s_name` VARCHAR(20) NOT NULL comment '姓名', `s_birth` VARCHAR(20) NOT NULL comment '生日', `s_sex` VARCHAR(10) NOT NULL comment '性別', PRIMARY KEY(`s_id`) ); --課程表 DROP TABLE IF EXISTS `course`; CREATE TABLE `course`( `c_id` VARCHAR(20), `c_name` VARCHAR(20) NOT NULL comment '課程名', `t_id` VARCHAR(20) NOT NULL comment '學生id', PRIMARY KEY(`c_id`) ); --教師表 DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher`( `t_id` VARCHAR(20), `t_name` VARCHAR(20) NOT NULL comment '教師名稱', PRIMARY KEY(`t_id`) ); --成績表 DROP TABLE IF EXISTS `score`; CREATE TABLE `score`( `s_id` VARCHAR(20), `c_id` VARCHAR(20) NOT NULL comment '課程名', `s_score` INT(3) NOT NULL comment '成績', PRIMARY KEY(`s_id`,`c_id`) ); --插入學生表測試數據 insert into student values('01' , '趙雷' , '1990-01-01' , '男'); insert into student values('02' , '錢電' , '1990-12-21' , '男'); insert into student values('03' , '孫風' , '1990-05-20' , '男'); insert into student values('04' , '李雲' , '1990-08-06' , '男'); insert into student values('05' , '周梅' , '1991-12-01' , '女'); insert into student values('06' , '吳蘭' , '1992-03-01' , '女'); insert into student values('07' , '鄭竹' , '1989-07-01' , '女'); insert into student values('08' , '王菊' , '1990-01-20' , '女'); --課程表測試數據 insert into course values('01' , '語文' , '02'); insert into course values('02' , '數學' , '01'); insert into course values('03' , '英語' , '03'); --教師表測試數據 insert into teacher values('01' , '張三'); insert into teacher values('02' , '李四'); insert into teacher values('03' , '王五'); --成績表測試數據 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);
一、查詢"01"課程比"02"課程成績高的學生的信息及課程分數code
select a.* ,b.s_score as 01_score,c.s_score as 02_score
from student a join score b
on a.s_id=b.s_id and b.c_id='01' left join score c
on a.s_id=c.s_id and c.c_id='02'
where b.s_score>c.s_score blog
查詢結果:數學
二、查詢"01"課程比"02"課程成績低的學生的信息及課程分數class
select a.* ,b.s_score as 01_score,c.s_score as 02_score
from student a join score b
on a.s_id=b.s_id and b.c_id='01' left join score c
on a.s_id=c.s_id and c.c_id='02'
where b.s_score<c.s_score
查詢結果:
三、查詢平均成績大於等於60分的同窗的學生編號和學生姓名和平均成績
select a.s_id,a.s_name,round(avg(b.s_score),2) as avg_score
from student a join score b on a.s_id=b.s_id
group by a.s_id,a.s_name having round(avg(b.s_score),2)>=60;
查詢結果:
四、查詢平均成績小於60分的同窗的學生編號和學生姓名和平均成績-- (包括有成績的和無成績的)
select a.s_id,a.s_name,round(avg(b.s_score),2) as avg_score
from student a join score b on a.s_id=b.s_id
group by a.s_id,a.s_name having round(avg(b.s_score),2)<60;
查詢結果:
五、查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績
select a.s_id as '學生編號',a.s_name as '學生姓名',
sum(b.c_id) as '選課總數',
sum(b.s_score) as '課程總成績'
from student a join score b on a.s_id=b.s_id
GROUP BY a.s_id,a.s_name
查詢結果:
六、查詢"李"姓老師的數量
select count(t_id) as '老師數量'
from teacher
where t_name LIKE '李%';
查詢結果:
七、查詢學過"張三"老師授課的同窗的信息
select a.* from
student a join score s on a.s_id = s.s_id
where s.c_id in(
select c_id from course where t_id = (
select t_id from teacher where t_name='張三'));
查詢結果:沒有符合結果
八、查詢沒學過"張三"老師授課的同窗的信息
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 = '張三')));
查詢結果:
九、查詢學過編號爲"01"而且也學過編號爲"02"的課程的同窗的信息
十、查詢學過編號爲"01"可是沒有學過編號爲"02"的課程的同窗的信息