建表:
-- 建表
-- 學(xué)生表
CREATE TABLE `Student`(
? ? `s_id` VARCHAR(20),
? ? `s_name` VARCHAR(20) NOT NULL DEFAULT '',
? ? `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
? ? `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
? ? PRIMARY KEY(`s_id`)
);
-- 課程表
CREATE TABLE `Course`(
? ? `c_id`? VARCHAR(20),
? ? `c_name` VARCHAR(20) NOT NULL DEFAULT '',
? ? `t_id` VARCHAR(20) NOT NULL,
? ? PRIMARY KEY(`c_id`)
);
-- 教師表
CREATE TABLE `Teacher`(
? ? `t_id` VARCHAR(20),
? ? `t_name` VARCHAR(20) NOT NULL DEFAULT '',
? ? PRIMARY KEY(`t_id`)
);
-- 成績表
CREATE TABLE `Score`(
? ? `s_id` VARCHAR(20),
? ? `c_id`? VARCHAR(20),
? ? `s_score` INT(3),
? ? PRIMARY KEY(`s_id`,`c_id`)
);
-- 插入學(xué)生表測試數(shù)據(jù)
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風(fēng)' , '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' , '女');
-- 課程表測試數(shù)據(jù)
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數(shù)學(xué)' , '01');
insert into Course values('03' , '英語' , '03');
-- 教師表測試數(shù)據(jù)
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成績表測試數(shù)據(jù)
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 "課程成績高的學(xué)生的信息及課程分數(shù)
select n1.s_id, score1, score2
from (select s_id, s_score as score1 from score where c_id=01) n1 join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id
where score1>score2;
1.1 查詢同時存在" 01 "課程和" 02 "課程的情況
select n1.s_id, score1, score2
from (select s_id, s_score as score1 from score where c_id=01) n1 join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;
1.2 查詢存在" 01 "課程但可能不存在" 02 "課程的情況(不存在時顯示為 null )
select n1.s_id, score1, score2
from (select s_id, s_score as score1 from score where c_id=01) n1 left join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;
1.3 查詢不存在" 01 "課程但存在" 02 "課程的情況
select n2.s_id, score1, score2
from (select s_id, s_score as score1 from score where c_id=01) n1 right join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;
題目二:查詢平均成績大于等于 60 分的同學(xué)的學(xué)生編號和學(xué)生姓名和平均成績
select st.s_id, st.s_name, avg(s_score) as avg_score
from student st join score sc on st.s_id=sc.s_id
group by st.s_id
having avg_score >= 60;
題目三:查詢在 SC 表存在成績的學(xué)生信息
select distinct st.s_id, st.s_name, avg(sc.s_score)
from student st join score sc on st.s_id=sc.s_id
group by st.s_id;
題目四:查詢所有同學(xué)的學(xué)生編號便监、學(xué)生姓名唐片、選課總數(shù)橘蜜、所有課程的總成績(沒成績的顯示為null)
select st.s_id, st.s_name, count(s_score) as num_course, sum(s_score) as sum_score
from student st left join score sc on st.s_id=sc.s_id
group by st.s_id;
?4.1 查有成績的學(xué)生信息
select st.s_id, st.s_name, count(s_score) as num_course, sum(s_score) as sum_score
from student st join score sc on st.s_id=sc.s_id
group by st.s_id;
題目五:查詢「李」姓老師的數(shù)量
select count(t_name) as num_teacher
from teacher
where t_name like '李%';
(或者使用regexp '李.')
題目六:查詢學(xué)過「張三」老師授課的同學(xué)的信息
select a.s_id, a.s_name,a.s_birth, a.s_sex, a.c_id, b.t_name
from (select st.s_id, st.s_birth, st.s_sex, s_name, c_id from student st join score sc on st.s_id=sc.s_id) a
join (select co.c_id, te.t_name from teacher te join course co on te.t_id=co.t_id) b on a.c_id=b.c_id
where b.t_name='張三';
經(jīng)過與他人答案對比烫止,有以下更簡單的方法:
select student.*
from student, score sc
where student.s_id = sc.s_id
and sc.c_id in (select c_id from course, teacher where course.t_id = teacher.t_id and T_name = '張三')
*6.1查詢學(xué)過編號為"01"并且也學(xué)過編號為"02"的課程的同學(xué)的信息
select * from student
where s_id in (select s_id from score where c_id=01)
and s_id in (select s_id from score where c_id=02);
6.2查詢學(xué)過編號為"01"但是沒有學(xué)過編號為"02"的課程的同學(xué)的信息
select * from student
where s_id in (select s_id from score where c_id=01)
and s_id not in (select s_id from score where c_id=02);
題目七:查詢沒有學(xué)全所有課程的同學(xué)的信息
select * from student
where s_id not in (select s_id from score where c_id=01)
or s_id not in (select s_id from score where c_id=02)
or s_id not in (select s_id from score where c_id=03);
(也可以使用count查詢學(xué)習(xí)課程數(shù)<總課程數(shù)的學(xué)生)
題目八:查詢至少有一門課與學(xué)號為" 01 "的同學(xué)所學(xué)相同的同學(xué)的信息
select distinct st.* from student st, score sc
where st.s_id=sc.s_id and sc.c_id in (select c_id from score where s_id=01);
***題目九:查詢和" 01 "號的同學(xué)學(xué)習(xí)的課程 完全相同的其他同學(xué)的信息
select st.* from student st where st.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(c_id)=(select count(c_id) from score where s_id='01'));
題目十:查詢沒學(xué)過"張三"老師講授的任一門課程的學(xué)生姓名
select st.s_id, st.s_name
from student st
where st.s_id not in (select s_id
from score sc where sc.c_id in (select c_id from course co, teacher te where co.t_id=te.t_id and te.t_name='張三'));
(思路:先找出學(xué)過張三老師任一門課程的同學(xué)陌选,將這些同學(xué)排除即可)